CDT*_*TWF 7 .net c# xml whitespace xmlserializer
我有一个类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; }
}
}
Run Code Online (Sandbox Code Playgroud)
使用......
FileStream fs = new FileStream(path, FileMode.Create);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(InputConfig));
xmlSerializer.Serialize(fs, this);
fs.Close();
Run Code Online (Sandbox Code Playgroud)
......而且......
StreamReader sr = new StreamReader(path);
XmlSerializer reader = new XmlSerializer(typeof(InputConfig));
InputConfig inputConfig = (InputConfig)reader.Deserialize(sr);
Run Code Online (Sandbox Code Playgroud)
它就像一个冠军!简单的东西,除了我需要function在反序列化时保留成员中的空格.生成的XML文件表明序列化时保留了空格,但在反序列化时丢失了.
<IncludeExcludeRules>
<IncludeExcludeRule>
<Idx>17</Idx>
<Name>LIEN</Name>
<Operation>E =</Operation>
<Function> </Function>
</IncludeExcludeRule>
</IncludeExcludeRules>
Run Code Online (Sandbox Code Playgroud)
XmlAttributeAttribute的MSDN文档似乎在标题备注下解决了这个问题,但我不明白如何使用它.它提供了这个例子:
// Set this to 'default' or 'preserve'.
[XmlAttribute("space",
Namespace = "http://www.w3.org/XML/1998/namespace")]
public string Space
Run Code Online (Sandbox Code Playgroud)
咦?设置"默认"或"保留"的内容?我确定我很接近,但这只是没有意义.我必须认为只有一行XmlAttribute在成员之前插入类中以在反序列化时保留空格.
在这里和其他地方有许多类似问题的实例,但它们似乎都涉及使用XmlReader和XmlDocument,或者涉及单个节点等.我想避免这种深度.
要在XML反序列化期间保留所有空格,只需创建并使用XmlReader:
StreamReader sr = new StreamReader(path);
XmlReader xr = XmlReader.Create(sr);
XmlSerializer reader = new XmlSerializer(typeof(InputConfig));
InputConfig inputConfig = (InputConfig)reader.Deserialize(xr);
Run Code Online (Sandbox Code Playgroud)
与之不同XmlSerializer.Deserialize(XmlReader),XmlSerializer.Deserialize(TextReader)仅保留由xml:space="preserve"属性标记的重要空白.
神秘的文档意味着您需要指定一个附加字段,其[XmlAttribute("space", Namespace = "http://www.w3.org/XML/1998/namespace")]值为default或preserve。XmlAttribute 控制为字段或属性生成的属性的名称。属性的值是字段的值。
例如这个类:
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";
}
Run Code Online (Sandbox Code Playgroud)
将被序列化为:
<?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" />
Run Code Online (Sandbox Code Playgroud)