Deserializing XML to Objects in C#

Create a class for each element that has a property for each element and a List or Array of objects (use the created one) for each child element. Then call System.Xml.Serialization.XmlSerializer.Deserialize on the string and cast the result as your object. Use the System.Xml.Serialization attributes to make adjustments, like to map the element to your ToDoList class, use the XmlElement(“todo-list”) attribute.

A shourtcut is to load your XML into Visual Studio, click the “Infer Schema” button and run “xsd.exe /c schema.xsd” to generate the classes. xsd.exe is in the tools folder. Then go through the generated code and make adjustments, such as changing shorts to ints where appropriate.

Leave a Comment