2

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

Gopi
  • 5,656
  • 22
  • 80
  • 146

2 Answers2

5

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;
        }
    }
Om Deshmane
  • 808
  • 6
  • 11
4

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);
    }
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900