I am trying to deserialize a XML, however I am having a lot of problems with the List object that I am using.
The XML is the following:
<EntriesSerialize>
<Entries>
<Entry file="myFile"
value="2000" />
<Entry file="myFile"
value="400" />
<Entry file="myFile"
value="200" />
</Entries>
</EntriesSerialize>
My classes are:
[XmlType("Entry")]
public class Entry
{
public Entry()
{
}
[XmlAttribute("file")]
public string File { get; set; }
[XmlAttribute("value")]
public string Value { get; set; }
}
[XmlType("EntriesSerialize")]
public class EntriesSerialize
{
public EntriesSerialize()
{
EntriesList = new List<Entry>();
}
[XmlElement("Entries")]
public List<Entry> EntriesList { get; set; }
}
The code that I am using to deserialize this XML is:
public static T Deserialize<T>(string content)
{
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(content);
MemoryStream stream = new MemoryStream(byteArray);
StreamReader reader = new StreamReader(stream);
XmlSerializer serializer;
serializer = new XmlSerializer(typeof(T));
T model = (T)serializer.Deserialize(reader);
reader.Close();
return model;
}
The method that I call in the deserialization is the Deserialize(string content) like this:
EntriesSerialize temp = Deserialize<EntriesSerialize>(data);
However when I look at the temp variable using the C# debugger, I see that there is only one element inside the EntriesList object and that this element has its file and value attributes as null.
Note: the serialization of those classes works as expected.
The attribute(s) on
EntriesListshould be:Your current syntax with
[XmlElement("Entries")]is configured for:(for info, neither of the
[XmlType("...")]attributes are used in this scenario, but they don’t do any harm either)