I have an XML file with the following format:
<?xml version="1.0" encoding="UTF-8"?>
<Items>
<Item Property1="A" Property2="B" />
<Item Property1="C" Property2="D" />
</Items>
I need to read the <Item>
elements as objects of class MyClass
using an XmlSerializer.
public class MyCLass
{
[XmlAttribute]
public string Property1 { get; set; }
[XmlAttribute]
public string Property2 { get; set; }
}
Currently, I have the following code to read the file:
XmlSerializer serializer =
new XmlSerializer(typeof(MyClass[]), new XmlRootAttribute(@"Items"));
MyClass[] list = (MyClass[])serializer.Deserialize(...);
Since the element name <Item>
is different from the class name MyCLass
, the elements in the array are not deserialized at all. The above code works if I rename MyClass
to Item
, but unfortunately I am not allowed to change the XML file or the class names.
How do I go about mapping the two so that the file can be read correctly?
Thanks in advance!