How do I map XML to C# objects - c#

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?

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.

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.

Related

Asp.net XML to objects

I have a XML file, with a structure like
<items>
<item>
<someDetail>
A value here
</someDetail>
</item>
<item>
<someDetail>
Another value here
</someDetail>
</item>
</items>
With multiple items in it.
I want to deserialize the XML on session start ideally, to turn the XML data to objects based on a class (c# asp.net 4)
I have tried several ways with either no success, or a solution which seems clunky and inelegant.
What would people suggest?
I have tried using the xsd.exe tool, and have tried with the xml reader class, as well as usin XElement class to loop through the xml and then create new someObject(props).
These maybe the best and/or only way, but with it being so easy for database sources using the entities framework, I wondered if there was a similar way to do the same but from a xml source.
The best way to deserialize XML it to create a class that corresponds to the XML structure into which the XML data will deserialize.
The latest serialization technology uses Data Contracts and the DataContractSerializer.
You decorate the class I mentioned above with DataMember and DataItem attributes and user the serializer to deserialize.
I'd use directly the .NET XML serialization - classes declarations:
public class Item {
[XmlElement("someDetail")]
public string SomeDetail;
} // class Item
[XmlRoot("items")]
public class MyData {
[XmlElement("item")]
public List<Item> Items;
public static MyData Deserialize(Stream source)
{
XmlSerializer serializer = new XmlSerializer(typeof(MyData));
return serializer.Deserialize(source) as MyData;
} // Deserialize
} // class MyData
and then to read the XML:
using (FileStream fs = new FileStream(#"c:\temp\items.xml", FileMode.Open, FileAccess.Read)) {
MyData myData = MyData.Deserialize(fs);
}
I've concluded is there is not simple unified mechanism (probably due to the inherent complexities involved with non trivial cases - this question always crops up in the context of simple scenarios like your example xml).
Xml serialization is pretty easy to use. For your example, you would just have to create a class to contain a items and another class for the actual item. You might have to apply some attributes to get everything to work correctly, but the coding will not be much. Then it's as easy as -
var serializer = new XmlSerializer(typeof(ItemsContainer));
var items = serializer.Deserialize(...) as ItemsContainer;
Datasets are sometimes considered "yesterday tech" but I use them when they solve the problem well, and you can leverage the designer. The generated code is not pretty but the bottom line is you can persist to a database via the auto generated adapters and to XML using a method right on the data set. You can read it in this way as well.
XSD.exe isn't that bad once you get used to it. I printed the help to a text file and included it in my solutions for a while. When you use the /c option to create classes, you get clean code that can be used with the XmlSerialzier.
Visual Studio 2010 (maybe other versions too) has an XML menu which appears when you have an Xml file open and from that you can also generate an XSD from sample Xml. So in a couple of steps you could take your example xml and generate the XSD, then run it through XSD.exe and use the generated classes with a couple of lines XmlSerializer code... it feels like a lot of machinations but you get used to it.

The xsd.exe generated C# class from the W3C XMLschema.xsd (the schema for xsd validation) is not accepted by the C# XmlSerializer

The utility 'xsd.exe' will generate c# class source code that corresponds in various ways to the information in an xsd schema file.
So, I download the schema file 'XMLSchema.xsd' located at "http://www.w3.org/2001/XMLSchema" -- this is the schema for the xsd files themselves.
I generate the C# class corresponding to the 'schema for schemas', using:
xsd.exe /classes /namespace:w3c XMLSchema.xsd
So far so good. I get a file 'XMLSchema.cs' containing a C# class 'schema', and other stuff, in namespace 'w3c' which I proceed to add to a C# project which also contains the following:
try
{
XmlSerializer loader = new XmlSerializer(typeof(w3c.schema));
//never here!! previous line throws!
FileStream fs = new FileStream(
#"M:\src\Interfaces\MyClass1.xsd", FileMode.Open, FileAccess.Read
);
object fromXml = loader.Deserialize(fs);
w3c.schema MyClass1Schema = (w3c.schema)fromXml;
}
catch(Exception e)
{
}
Unfortunately, it throws the following error on the first line of the try block:
The XML element 'annotation' from
namespace
'http://www.w3.org/2001/XMLSchema' is
already present in the current scope.
Use XML attributes to specify another
XML name or namespace for the element.
Has anyone experienced this error?
I would rather not make any modification to the generated file 'XMLSchema.cs'.
I have also (originally) tried:
xsd /classes XMLSchema.xsd
(and no namespacing in the C# test code) with the same result.
I'm going to guess that the problem has nothing to do with .NET namespaces, as you guessed with your second command, but with XML namespaces.
The problem it appears you're having is that the XSD file defines a namespace (likely, xsd) that the XML Serializer already uses (for, surprise surprise, the XSD for the XSD).
I am not sure what the XML spec says for two equivalent namespaces with different identifiers, but the proper way to resolve this would be to change the namespace of your input XSD file. Of course, this will render it invalid, but it would stop XmlSerializer from throwing, I think.
I believe you cannot use namespace in typeof(), instead put the name of the object (instance of class) that you are trying to serialize.

Question on usage of XML with XPath vs XML as Class

I have few XML files which I would be using in my C# code.
So far I have been using XPATH for accessing the XML node / attributes
Question is what advantage would I get if i convert the XML to Class file (XSD.EXE) and use it in terms of maintainability and code readability.
In both the cases I know if I add or remove some nodes, code needs to be changed
In my case the DLL goes into GAC.
I am just trying to get your views
Cheers,
Karthik
The beauty of converting your XML to XSD and then to a C# class is the ease in which you can grab yet another file. Your code would be something like:
XmlSerializer ser = new XmlSerializer(typeof(MyClass));
FileStream fstm = new FileStream(#"C:\mysample.xml", FileMode.Open, FileAccess.Read);
MyClass result = ser.Deserialize(fstm) as MyClass;
if(result != null)
{
// do whatever you want with your new class instance!
}
With these few lines, you now have an object that represent exactly what your XML contained, and you can access its properties as properties on the object instance - much easier than doing lots of complicated XPath queries into your XML, in my opinion.
Also, thanks to the fact you now have a XSD, you can also easily validate incoming XML files to make sure they actually do correspond to the contract defined - which causes less constant error-checking in your code (you don't have to check after each XPath to see whether there's any node(s) that actually match that expression etc.).

How can I transform an object graph to an external XML format

I have to send information too a third party in an XML format they have specified, a very common task I'm sure.
I have set of XSD files and, using XSD.exe, I have created a set of types. To generate the XML I map the values from the types within my domain to the 3rd party types:
public ExternalBar Map(InternalFoo foo) {
var bar = new ExternalBar;
bar.GivenName = foo.FirstName;
bar.FamilyName = foo.LastName;
return bar;
}
I will then use the XMLSerializer to generate the files, probably checking them against the XSD before releasing them.
This method is very manual though and I wonder if there is a better way using the Framework or external tools to map the data and create the files.
LINQ to XML works quite well for this... e.g.
XElement results = new XElement("ExternalFoos",
from f in internalFoos
select new XElement("ExternalFoo", new XAttribute[] {
new XAttribute("GivenName", f.FirstName),
new XAttribute("FamilyName", f.LastName) } ));
Firstly, I'm assuming that the object properties in your existing domain map to the 3rd party types without much manipulation, except for the repetitive property assignments.
So I'd recommend just using standard XML serialization of your domain tree (generate an outbound schema for your classes using XSD), then post-processing the result via a set of XSLT stylesheets. Then after post-processing, validate the resulting XML documents against the 3rd party schemas.
It'll probably be more complicated than that, because it really depends on the complexity of the mapping between the object domains, but this is a method that I've used successfully in the past.
As far as GUI tools are concerned I've heard (but not used myself) that Stylus Studio is pretty good for schema-to-schema mappings (screenshot here).

Fill class properties generated from XSD.exe

OK, I generated C# classes from my huge XSD file. Now I have a set of C# classes, XSD schema and actual XML data. Is there an automatic or semi-automatic way to fill these class instances with XML data that I have?
Thank you.
If you have used xsd.exe to generate the classes, then XmlSerializer should do the job...
XmlSerializer ser = new XmlSerializer(typeof(RootType));
RootType type = (RootType) ser.Deserialize(source)
You use the xml serialization/deserialization to export/import data to xml. Take a look at the XmlSerializer class. An example is on the msdn page.

Categories

Resources