【问题标题】:Retaining order of the xml elements between serialization and deserialization in C#在C#中序列化和反序列化之间保留xml元素的顺序
【发布时间】:2014-06-02 10:50:39
【问题描述】:

我已经从 Xml 模式生成了一个 C# 类。此类用于执行以下操作的工具:

  1. 通过反序列化读取 Xml
  2. 对反序列化的对象进行某些更改。
  3. 通过序列化将处理后的反序列化对象写入Xml。

基本上我在xml中填充了一些数据。

我在反序列化和序列化之间保留某些元素的顺序时遇到问题。

我有下面的示例代码。 fPNodeTypeWindowField 和 fPNodeTypeClass1Field 可以在两个 xml 中互换出现。这些字段的顺序在 Xml 中是不固定的。在反序列化时阅读 Xml 时我没有遇到任何问题。这些字段的顺序遵循代码中提到的顺序。

是否有任何可能的解决方案来保留这些字段在 Xml 中的顺序?

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class FPNodeTypeClass
{

    private string helpTextField;

    private FpxFPTreeFPNodeTypeRootFPNodeTypeWindow[] fPNodeTypeWindowField;

    private FPNodeTypeClass[] fPNodeTypeClass1Field;

    private string nameField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string HelpText
    {
        get
        {
            return this.helpTextField;
        }
        set
        {
            this.helpTextField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("FPNodeTypeWindow")]
    public FpxFPTreeFPNodeTypeRootFPNodeTypeWindow[] FPNodeTypeWindow
    {
        get
        {
            return this.fPNodeTypeWindowField;
        }
        set
        {
            this.fPNodeTypeWindowField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("FPNodeTypeClass")]
    public FPNodeTypeClass[] FPNodeTypeClass1
    {
        get
        {
            return this.fPNodeTypeClass1Field;
        }
        set
        {
            this.fPNodeTypeClass1Field = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField = value;
        }
    }
}

【问题讨论】:

    标签: c# .net xml serialization


    【解决方案1】:

    我刚刚发现自己可以在序列化时修复顺序。它不能是动态的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      • 2012-02-02
      • 2016-02-04
      • 1970-01-01
      • 2011-05-12
      • 2011-04-12
      • 2021-11-28
      相关资源
      最近更新 更多