简体   繁体   English

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

[英]XmlSerializer - Same Element with different Attributes

Expected XML Output: 预期的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>

To get the above XML document, I designed the following class 为了获得上述XML文档,我设计了以下类

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

Name class will be 名称类将是

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

XmlSerializer Code: XmlSerializer代码:

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

This give me the following output 这给了我以下输出

<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>

Here the field2 should be field I know I need to change the field2 as field in doc class but that results in error. 这里field2应该是字段我知道我需要将field2更改为doc类中的字段 ,但这会导致错误。

How should I design my class to get the expected output? 我应该如何设计我的课程以获得预期的输出?

Edit: ID class will also look like Name class with its own attributes 编辑: ID类也将看起来像具有自己的属性的Name类

Two options 两种选择

 [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

this will produce elements in given order. 这将按给定的顺序产生元素。 Or use arrays like 或者使用类似的数组

[XmlRoot("doc")]
    public class Doc
    {
        [XmlArray("field")]
        public Field[] Fields
        {
            get;
            set;
        }
    }

Something like: 就像是:

[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; }
}

Then: 然后:

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM