Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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);
}

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

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.

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

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

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

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

I'll bet NetDataContractSerializer can do what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top