XML
<MeterWalkOrder>
<Name>Red Route</Name>
<Meters>
<Meter>
<MeterID>1</MeterID>
<SerialNumber>12345</SerialNumber>
</Meter>
<Meter>
<MeterID>2</MeterID>
<SerialNumber>SE</SerialNumber>
</Meter>
</Meters>
</MeterWalkOrder>
I cannot get simple XML into an object using any serializer
var xml = File.ReadAllText("WalkOrder.xml");
var xmlSerializer = new NFormats.Xml.XmlSerializer();
var obj = xmlSerializer.Deserialize<MeterWalkOrder>(new StringReader(xml));
I just get back 2 meter objects that have none of the attributes set and the name is not even set in walk order.
public partial class MeterWalkOrder
{
public MeterWalkOrder()
{
Meters = new List<Meter>();
}
[DataMember]
public String Name { get; set; }
}
}
using System;
using System.Xml.Serialization;
namespace WindowsFormsApplication1.Classes
{
public class Meter : IMeter
{
[XmlAttribute]
public int MeterID { get; set; }
[XmlAttribute]
public String SerialNumber { get; set; }
}
}
I am willing to try any xml serializer.
I used your sample XML and generated the classes inside VisualStudio with the
Paste Special -> Paste XML as Classes
, modified them a little bit to make them more readable and got the following class definitions from it:using the above classes and the below code, it generated the objects without errors.
First of all i suggest you to read Introducing XML Serialization on MSDN You made a couple of errors which lead to not mentioned exceptions thrown when you run your code.
Find a fully working example below: