Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xmlNode to objects

I have been working with a 3rd party java based REST webservice, that returns an array of xmlNodes.

The xmlNode[] respresent an object and I am trying to work out the best way to Deserialize the xmlNode[] in the object? is it to build up a xmlDocument first and the Deserialize ?

Thanks

like image 560
76mel Avatar asked Oct 13 '09 23:10

76mel


People also ask

What is an XmlNode?

According to the XML DOM, everything in an XML document is a node: The entire document is a document node. Every XML element is an element node. The text in the XML elements are text nodes. Every attribute is an attribute node.

What is XML node in C#?

XmlNode is the base class in the . NET implementation of the DOM. It supports XPath selections and provides editing capabilities. The XmlDocument class extends XmlNode and represents an XML document. You can use XmlDocument to load and save XML data.


2 Answers

If you have the WCF Rest Starter Kit preview installed, there's a neat trick:

  • open Visual Studio
  • select your XML node contents (the XML that makes up one of your nodes) and copy it to the clipboard
  • from your "Edit" menu in Visual Studio, pick "Paste XML as Types"

This will paste your XML that's on the clipboard into your project as a C# class that is capable of deserializing that exact XML. Pretty nifty!

See these blog posts about it:

  • Aaron Skonnard: WCF REST Starter Kit: Paste XML as Types
  • "Paste XML as Types" in REST Starter Kit

That should save you a lot of typing and make life a lot easier!

UPDATE:
OK, you already have your classes generated from the XML you get back. Now you need to convert a XmlNode to your class.

You'll have to do something like this:

private static T ConvertNode<T>(XmlNode node) where T: class
{
    MemoryStream stm = new MemoryStream();

    StreamWriter stw = new StreamWriter(stm);
    stw.Write(node.OuterXml);
    stw.Flush();

    stm.Position = 0;

    XmlSerializer ser = new XmlSerializer(typeof(T));
    T result = (ser.Deserialize(stm) as T);

    return result;
}

You need to write the XML representation (property .OuterXml) of the XmlNode to a stream (here a MemoryStream) and then use the XmlSerializer to serialize back the object from that stream.

You can do it with the generic method and call

 Customer myCustomer = ConvertNode<Customer>(xmlNode);

or you could even turn that code into either an extension method on the XmlNode class so you could write:

 Customer myCustomer = xmlNode.ConvertNode<Customer>();

Marc

like image 130
marc_s Avatar answered Sep 20 '22 15:09

marc_s


Maybe this is too late to answer here but it will help others:

Here is the solution you will be able to Deserialize from the XML node.

 XmlDocument xmlDoc = new XmlDocument();
 xmlDoc.LoadXml(xml);

 XmlNode xmlNode = xmlDoc.SelectSingleNode("//SystemInfo");

 XmlSerializer serial = new XmlSerializer(typeof(SystemInfo));
 
 using(XmlNodeReader reader = new XmlNodeReader(xmlNode)) {
     SystemInfo syso =(SystemInfo)serial.Deserialize(reader);
 }

The first load the XML to XmlDocument Object and then find the parent node you will wish to deserialize just like I want SystemInfo object node from all the XML document.

Once you find that create an XmlSerializer object with the specific class type you will wish to.

Now just pass the reader (created with using) to the Deserialize method, you will get the objects populated in the class object just like I populated syso object with XML values.

Happy Coding :)

like image 38
Khawaja Asim Avatar answered Sep 22 '22 15:09

Khawaja Asim