I have a xml file that I want to map to a C# class
The xml looks like this:
<root>
<table>
<fields>
<field name="createdby">Thomas</field>
<field name="id">123</field>
<field name="Title">New Article</field>
</fields>
</table>
</root>
And I have created some classes to map this, not sure if this is the shortest way to do that:
Root:
[XmlRoot(ElementName = "root")]
public class Root
{
[XmlElement(ElementName = "table")]
public Table Table { get; set; }
}
Table:
[XmlRoot(ElementName = "table")]
public class Table
{
[XmlElement(ElementName = "fields")]
public Fields Fields { get; set; }
//[XmlAttribute(AttributeName = "name")]
//public string Name { get; set; }
//[XmlElement(ElementName = "table")]
//public List<Table> TableList { get; set; }
}
Fields:
[XmlRoot(ElementName = "fields")]
public class Fields
{
[XmlElement(ElementName = "field")]
public List<Field> Field { get; set; }
}
Field:
[XmlRoot(ElementName = "field")]
public class Field
{
//Would like to the the createdby name here "Thomas"
[XmlAttribute(AttributeName="createdby")]
public string Name { get; set; }
[XmlAttribute(AttributeName="id")]
public int Id { get; set; }
[XmlAttribute(AttributeName="title")]
public string Title { get; set; }
}
is this the right approach ??
Hope someone can help and maybe find a clean way.. :P
The goal is to have a class called
public class Article
{
public string Name { get; set; }
public string Id { get; set; }
public string CreatedBy { get; set; }
public string Content { get; set; }
}
Where I can map data values from the xml file to this. Something like this.
Article article = new Article();
Article.CreatedBy = result.Table.Fields.Field.Createdby