How do I map XML to C# objects

67,045

Solution 1

You can generate serializable C# classes from a schema (xsd) using xsd.exe:

xsd.exe dependency1.xsd dependency2.xsd schema.xsd /out:outputDir

If the schema has dependencies (included/imported schemas), they must all be included on the same command line.

Solution 2

LINQ to XML is very powerful if you're using .net 3.5, LINQ to XSD may be useful to you too!

Solution 3

Use xsd.exe command line program that comes with visual studio to create class files that you can use in your project/solution, and the System.Xml.Serialization namespace (specifically, the XmlSerializer class) to serialize/deserialze those classes to and from disk.

Solution 4

I agree xsd is really crap... But they made another version that hardly anyone knows about. Its called xsd object generator. Its the next version and has way more options. It generates files from XSD and works fantastic. If you have a schema generator like XML spy; create an xsd from your xml and use this tool. I have created very very complex classes using this tool. Then create partial classes for extra properties\methods etc, then when you update your schema you just regen your classes and any edits persist in your partial classes.

http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=7075

Solution 5

xsd.exe from Microsoft has a lot of bugs :| Try this open source pearl http://xsd2code.codeplex.com/

Share:
67,045
Seth
Author by

Seth

Updated on September 17, 2020

Comments

  • Seth
    Seth over 3 years

    I have an XML that I want to load to objects, manipulate those objects (set values, read values) and then save those XMLs back. It is important for me to have the XML in the structure (xsd) that I created.

    One way to do that is to write my own serializer, but is there a built in support for it or open source in C# that I can use?