views:

8667

answers:

7

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?

A: 

This microsoft tutorial should do it for you: http://microsoft.apress.com/asptodayarchive/73787/mapping-xml-to-c-objects-using-reflection

Mostlyharmless
A: 

I'll bet NetDataContractSerializer can do what you want.

dcstraw
+4  A: 

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

kronoz
This is now the official page for LINQ to XSD: http://linqtoxsd.codeplex.com/
Ryan Versaw
Does it really do mapping to c# as stated in the question?
Konstantin
nah, Linq To XML cannot be used to map to persistent classes.
Genady Sergeev
+1  A: 

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.

Joel Coehoorn
+4  A: 

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.

ckarras
+1  A: 

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

A: 

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.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;


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

Then to deserialze:

public static object DeSerializeFromXMLString(System.Type TypeToDeserialize, string xmlString)
{
    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(xmlString);
    MemoryStream mem = new MemoryStream(bytes);   
    System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(TypeToDeserialize);
    return ser.Deserialize(mem);
}
Handy routine, but note that it won't work on objects that do not have a parameterless constructor.
Velika