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

发布于 2024-10-07 15:58:15 字数 1780 浏览 2 评论 0原文

预期的 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< /strong> 应该是 field 我知道我需要将 field2 更改为 doc 类中的 field 但结果是错误中。

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

编辑: ID 类也将类似于具有自己的属性的 Name 类

Expected XML Output:

<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

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

How should I design my class to get the expected output?

Edit: ID class will also look like Name class with its own attributes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纵性 2024-10-14 15:58:15

两个选项

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

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;
        }
    }
寄居者 2024-10-14 15:58:15

类似于:

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

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文