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