29

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?

9 Answers 9

29

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.

1
  • 12
    [+1] It's worth noting that you should also either use /c to generate classes or /d to generate a DataSet derived class when using an XSD. Otherwise you'll get the following error message "Error: Can only generate one of classes or datasets.".
    – Marc
    Dec 6, 2010 at 10:30
18

This code (C# DotNet 1.0 onwards) works quite well to serialize most objects to XML. (and back) It does not work for objects containing ArrayLists, and if possible stick to using only Arrays

using System;    
using System.IO;
using System.Text;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;


public static string Serialize(object objectToSerialize)
{
    MemoryStream mem = new MemoryStream();          
    XmlSerializer ser = new XmlSerializer(objectToSerialize.GetType());         
    ser.Serialize(mem, objectToSerialize);                      
    ASCIIEncoding ascii = new ASCIIEncoding();
    return ascii.GetString(mem.ToArray());
}        

public static object Deserialize(Type typeToDeserialize, string xmlString)
{
    byte[] bytes = Encoding.UTF8.GetBytes(xmlString);
    MemoryStream mem = new MemoryStream(bytes);         
    XmlSerializer ser = new XmlSerializer(typeToDeserialize);
    return ser.Deserialize(mem);
}
3
  • 1
    Handy routine, but note that it won't work on objects that do not have a parameterless constructor.
    – Chad
    Sep 29, 2009 at 3:57
  • do we need to write any kind of mapping tags on the class TypeToDeserialize and its memebers???
    – Mubashar
    Mar 22, 2011 at 7:29
  • This helped us convert between JSON.NET JSON and XML saved to the database. JSON.NET didn't create XML that could be re-maped back to JSON again using itself, i.e., we never figured out a way to use it's generated XML with ASP.NET's XML methods. Apr 11, 2013 at 14:19
7

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

2
  • Does it really do mapping to c# as stated in the question?
    – Konstantin
    Dec 16, 2009 at 14:07
  • nah, Linq To XML cannot be used to map to persistent classes. May 12, 2010 at 7:55
2

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.

2

using System.Xml.Serialization; this namespace has all the attributes you'll need if you want to map your xml to any random object. Alternatively you can use the xsd.exe tool

xsd file.xsd {/classes | /dataset} [/element:element] [/language:language] [/namespace:namespace] [/outputdir:directory] [URI:uri] which will take your xsd files and create c# or vb.net classes out of them.

http://msdn.microsoft.com/en-us/library/x6c1kb0s(VS.71).aspx

1

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

1

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

1
  • +1 thanks for this. It's actually a project that's active and has some documentation. Mar 6, 2013 at 16:19
1

We have created a framework which can auto-generate C# classes out of your XML. Its a visual item template to which you pass your XML and the classes are generated automatically in your project. Using these classes you can create/read/write your XML.

Check this link for the framework and Visual C# item template: click here

0

I'll bet NetDataContractSerializer can do what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.