使用XmlSerializer反序列化XML时,保留仅空白元素的内容 [英] Preserve whitespace-only element content when deserializing XML using XmlSerializer

查看:34
本文介绍了使用XmlSerializer反序列化XML时,保留仅空白元素的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 InputConfig 类,其中包含一个 List< IncludeExcludeRule> :

I have a class InputConfig which contains a 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文件表明在序列化时保留了空白,但是在反序列化时会丢失空白.

It works like a champ! Easy stuff, except that I need to preserve whitespace in the member function when deserializing. The generated XML file demonstrates that the whitespace was preserved when serializing, but it is lost on deserializing.

<IncludeExcludeRules>
  <IncludeExcludeRule>
    <Idx>17</Idx>
    <Name>LIEN</Name>
    <Operation>E =</Operation>
    <Function>  </Function>
  </IncludeExcludeRule>
</IncludeExcludeRules>

MSDN文档XmlAttributeAttribute 的名称似乎在标题备注下解决了此问题,但我不知道如何使用它.它提供了以下示例:

The MSDN documentation for XmlAttributeAttribute seems to address this very issue under the header Remarks, yet I don't understand how to put it to use. It provides this example:

// Set this to 'default' or 'preserve'.
[XmlAttribute("space", 
Namespace = "http://www.w3.org/XML/1998/namespace")]
public string Space 

嗯?将什么设置为默认"或保留"?我确定我已经接近了,但这是没有道理的.我必须认为只有XmlAttribute一行可以插入成员之前的类中,以在反序列化时保留空白.

Huh? Set what to 'default' or 'preserve'? I'm sure I'm close, but this just isn't making sense. I have to think there's just a single line XmlAttribute to insert in the class before the member to preserve whitespace on deserialize.

在这里和其他地方有很多类似问题的实例,但它们似乎都涉及使用XmlReader和XmlDocument,或者与单个节点等混为一谈.我想避免这种深度.

There are many instances of similar questions here and elsewhere, but they all seem to involve the use of XmlReader and XmlDocument, or mucking about with individual nodes and such. I'd like to avoid that depth.

推荐答案

要在XML反序列化期间保留所有空格,只需创建并使用 XmlReader :

To preserve all whitespace during XML deserialization, simply create and use an 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)不同,code> XmlSerializer.Deserialize(TextReader) 仅保留由 xml:space ="preserve" 属性标记的有效空白.

Unlike XmlSerializer.Deserialize(XmlReader), XmlSerializer.Deserialize(TextReader) preserves only significant whitespace marked by the xml:space="preserve" attribute.

这篇关于使用XmlSerializer反序列化XML时,保留仅空白元素的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆