XmlSerializer - 具有不同属性的相同元素

时间:2010-12-14 10:59:49

标签: c# xml-serialization xmlserializer

预期的XML输出:

<add>
 <doc>
  <field name="id">1</field>
  <field name="Myname">MyName1</field1>
 </doc>
 <doc>
  <field name="id">2</field>
  <field name="Myname">MyName2</field>
 </doc>
 <doc>
  <field name="id">3</field>
  <field name="Myname">MyName3</field>
 </doc>
</add>

为了获得上述XML文档,我设计了以下类

public class doc
{
    [XmlElement("field")]
    public ID Id
    {
        get;
        set;
    }
    [XmlElement("field2")]
    public Name Myname
    {
        get;
        set;
    }
}

名称类将是

 public class Name 
{
    [XmlText]
    public string Namevalue
    {
        get;
        set;
    }
    [XmlAttribute("name")]
    public string Myname
    {
        get;
        set;
    } 
}

XmlSerializer代码:

XmlSerializer serializer = new XmlSerializer(typeof(List<doc>), new XmlRootAttribute("add"));

这给我以下输出

<add>
 <doc>
  <field name="id">1</field>
  <field2 name="Myname">MyName1</field2>
 </doc>
 <doc>
  <field name="id">2</field>
  <field2 name="Myname">MyName2</field2>
 </doc>
 <doc>
  <field name="id">3</field>
  <field2 name="Myname">MyName3</field2>
 </doc>
</add>

此处 field2 应为字段我知道我需要在 field2 更改为字段 > doc 类,但会导致错误。

我应该如何设计我的课程以获得预期的输出?

编辑: ID类也将看起来像具有自己属性的Name类

2 个答案:

答案 0 :(得分:5)

两个选项

 [XmlRoot("doc")]
    public class Doc
    {
        [XmlElement("field",Order = 1)]
        public Field Id
        {
            get;
            set;
        }
        [XmlElement("field", Order = 2)]
        public Field Name
        {
            get;
            set;
        }
    }

    [XmlRoot("doc")]
    public class Field
    {
        [XmlText]
        public string Value
        {
            get;
            set;
        }

        [XmlAttribute("name")]
        public string Name
        {
            get;
            set;
        }
    }
enter code here

这将按给定顺序生成元素。或者使用像

这样的数组
[XmlRoot("doc")]
    public class Doc
    {
        [XmlArray("field")]
        public Field[] Fields
        {
            get;
            set;
        }
    }

答案 1 :(得分:4)

类似的东西:

[XmlType("add"), XmlRoot("add")]
public class WhateverAddIs {
    private readonly List<Document> docs = new List<Document>();
    [XmlElement("doc")]
    public List<Document> Documents { get { return docs; } }
}
public class Document {
    private readonly List<Field> fields = new List<Field>();
    [XmlElement("field")]
    public List<Field> Fields { get { return fields; } }
}
public class Field {
    [XmlAttribute("name")]
    public string Name { get; set; }
    [XmlText]
    public string Value { get; set; }
}

然后:

class Program {
    static void Main() {
        var add = new WhateverAddIs {
            Documents = {
                new Document {
                    Fields = {
                        new Field { Name="id", Value ="1"},
                        new Field { Name="Myname", Value ="Myname1"},
                    }                        
                }, new Document {
                    Fields = {
                        new Field { Name="id", Value ="2"},
                        new Field { Name="Myname", Value ="Myname2"},
                    }
                }, new Document {
                    Fields = {
                        new Field { Name="id", Value ="3"},
                        new Field { Name="Myname", Value ="Myname3"},
                    }
                }
            }
        };
        var ser = new XmlSerializer(add.GetType());
        ser.Serialize(Console.Out, add);
    }
}