使用 XmlSerializer 反序列化 XML 时保留 whitespace-only 元素内容
Preserve whitespace-only element content when deserializing XML using XmlSerializer
我有一个 class InputConfig
其中包含一个 List<IncludeExcludeRule>
:
public class InputConfig
{
// The rest of the class omitted
private List<IncludeExcludeRule> includeExcludeRules;
public List<IncludeExcludeRule> IncludeExcludeRules
{
get { return includeExcludeRules; }
set { includeExcludeRules = value; }
}
}
public class IncludeExcludeRule
{
// Other members omitted
private int idx;
private string function;
public int Idx
{
get { return idx; }
set { idx = value; }
}
public string Function
{
get { return function; }
set { function = value; }
}
}
使用...
FileStream fs = new FileStream(path, FileMode.Create);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(InputConfig));
xmlSerializer.Serialize(fs, this);
fs.Close();
...和...
StreamReader sr = new StreamReader(path);
XmlSerializer reader = new XmlSerializer(typeof(InputConfig));
InputConfig inputConfig = (InputConfig)reader.Deserialize(sr);
它就像冠军一样工作!简单的事情,除了反序列化时我需要在成员 function
中保留空格。生成的 XML 文件表明在序列化时保留了空格,但在反序列化时丢失了。
<IncludeExcludeRules>
<IncludeExcludeRule>
<Idx>17</Idx>
<Name>LIEN</Name>
<Operation>E =</Operation>
<Function> </Function>
</IncludeExcludeRule>
</IncludeExcludeRules>
MSDN documentation for XmlAttributeAttribute 似乎在 header 备注 下解决了这个问题,但我不明白如何使用它。它提供了这个例子:
// Set this to 'default' or 'preserve'.
[XmlAttribute("space",
Namespace = "http://www.w3.org/XML/1998/namespace")]
public string Space
嗯?将什么设置为 'default' 或 'preserve'?我确定我很接近,但这没有意义。我不得不认为只有一行 XmlAttribute 可以插入成员之前的 class 以在反序列化时保留空格。
这里和其他地方有很多类似问题的实例,但它们似乎都涉及 XmlReader 和 XmlDocument 的使用,或者处理单个节点等。我想避免那个深度。
我认为您缺少的部分是将 xml:space="preserve"
添加到字段中,例如:
<Function xml:space="preserve"> </Function>
有关详细信息,请参阅 XML Specification
中的相关部分
在class定义中加上注释,根据MSDN博客应该是:
[XmlAttribute("space=preserve")]
但我记得是
[XmlAttribute("xml:space=preserve")]
要在 XML 反序列化期间保留所有空白,只需创建并使用 XmlReader
:
StreamReader sr = new StreamReader(path);
XmlReader xr = XmlReader.Create(sr);
XmlSerializer reader = new XmlSerializer(typeof(InputConfig));
InputConfig inputConfig = (InputConfig)reader.Deserialize(xr);
与 XmlSerializer.Deserialize(XmlReader)
不同,XmlSerializer.Deserialize(TextReader)
仅保留由 xml:space="preserve"
属性标记的重要空白。
神秘的文档意味着您需要使用 [XmlAttribute("space", Namespace = "http://www.w3.org/XML/1998/namespace")]
指定一个附加字段,其值为 default
或 preserve
。 XmlAttribute 控制字段的生成属性的名称或 属性。属性的值是字段的值。
例如,这个 class:
public class Group
{
[XmlAttribute (Namespace = "http://www.cpandl.com")]
public string GroupName;
[XmlAttribute(DataType = "base64Binary")]
public Byte [] GroupNumber;
[XmlAttribute(DataType = "date", AttributeName = "CreationDate")]
public DateTime Today;
[XmlAttribute("space", Namespace = "http://www.w3.org/XML/1998/namespace")]
public string Space ="preserve";
}
将序列化为:
<?xml version="1.0" encoding="utf-16"?>
<Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
d1p1:GroupName=".NET"
GroupNumber="ZDI="
CreationDate="2001-01-10"
xml:space="preserve"
xmlns:d1p1="http://www.cpandl.com" />
Michael Liu 的上述回答对我有用,但有一个警告。我会评论他的回答,但我的 "reputation" 还不够。
我发现使用 XmlReader 并没有完全解决这个问题,原因是有问题的 .net 属性 具有属性:
XmlText(DataType="normalizedString")
为了纠正这个问题,我发现添加附加属性有效:
[XmlAttribute("xml:space=preserve")]
显然,如果您无法控制 .net class 那么您就有问题了。
我有一个 class InputConfig
其中包含一个 List<IncludeExcludeRule>
:
public class InputConfig
{
// The rest of the class omitted
private List<IncludeExcludeRule> includeExcludeRules;
public List<IncludeExcludeRule> IncludeExcludeRules
{
get { return includeExcludeRules; }
set { includeExcludeRules = value; }
}
}
public class IncludeExcludeRule
{
// Other members omitted
private int idx;
private string function;
public int Idx
{
get { return idx; }
set { idx = value; }
}
public string Function
{
get { return function; }
set { function = value; }
}
}
使用...
FileStream fs = new FileStream(path, FileMode.Create);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(InputConfig));
xmlSerializer.Serialize(fs, this);
fs.Close();
...和...
StreamReader sr = new StreamReader(path);
XmlSerializer reader = new XmlSerializer(typeof(InputConfig));
InputConfig inputConfig = (InputConfig)reader.Deserialize(sr);
它就像冠军一样工作!简单的事情,除了反序列化时我需要在成员 function
中保留空格。生成的 XML 文件表明在序列化时保留了空格,但在反序列化时丢失了。
<IncludeExcludeRules>
<IncludeExcludeRule>
<Idx>17</Idx>
<Name>LIEN</Name>
<Operation>E =</Operation>
<Function> </Function>
</IncludeExcludeRule>
</IncludeExcludeRules>
MSDN documentation for XmlAttributeAttribute 似乎在 header 备注 下解决了这个问题,但我不明白如何使用它。它提供了这个例子:
// Set this to 'default' or 'preserve'.
[XmlAttribute("space",
Namespace = "http://www.w3.org/XML/1998/namespace")]
public string Space
嗯?将什么设置为 'default' 或 'preserve'?我确定我很接近,但这没有意义。我不得不认为只有一行 XmlAttribute 可以插入成员之前的 class 以在反序列化时保留空格。
这里和其他地方有很多类似问题的实例,但它们似乎都涉及 XmlReader 和 XmlDocument 的使用,或者处理单个节点等。我想避免那个深度。
我认为您缺少的部分是将 xml:space="preserve"
添加到字段中,例如:
<Function xml:space="preserve"> </Function>
有关详细信息,请参阅 XML Specification
中的相关部分在class定义中加上注释,根据MSDN博客应该是:
[XmlAttribute("space=preserve")]
但我记得是
[XmlAttribute("xml:space=preserve")]
要在 XML 反序列化期间保留所有空白,只需创建并使用 XmlReader
:
StreamReader sr = new StreamReader(path);
XmlReader xr = XmlReader.Create(sr);
XmlSerializer reader = new XmlSerializer(typeof(InputConfig));
InputConfig inputConfig = (InputConfig)reader.Deserialize(xr);
与 XmlSerializer.Deserialize(XmlReader)
不同,XmlSerializer.Deserialize(TextReader)
仅保留由 xml:space="preserve"
属性标记的重要空白。
神秘的文档意味着您需要使用 [XmlAttribute("space", Namespace = "http://www.w3.org/XML/1998/namespace")]
指定一个附加字段,其值为 default
或 preserve
。 XmlAttribute 控制字段的生成属性的名称或 属性。属性的值是字段的值。
例如,这个 class:
public class Group
{
[XmlAttribute (Namespace = "http://www.cpandl.com")]
public string GroupName;
[XmlAttribute(DataType = "base64Binary")]
public Byte [] GroupNumber;
[XmlAttribute(DataType = "date", AttributeName = "CreationDate")]
public DateTime Today;
[XmlAttribute("space", Namespace = "http://www.w3.org/XML/1998/namespace")]
public string Space ="preserve";
}
将序列化为:
<?xml version="1.0" encoding="utf-16"?>
<Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
d1p1:GroupName=".NET"
GroupNumber="ZDI="
CreationDate="2001-01-10"
xml:space="preserve"
xmlns:d1p1="http://www.cpandl.com" />
Michael Liu 的上述回答对我有用,但有一个警告。我会评论他的回答,但我的 "reputation" 还不够。
我发现使用 XmlReader 并没有完全解决这个问题,原因是有问题的 .net 属性 具有属性:
XmlText(DataType="normalizedString")
为了纠正这个问题,我发现添加附加属性有效:
[XmlAttribute("xml:space=preserve")]
显然,如果您无法控制 .net class 那么您就有问题了。