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
If you have the WCF Rest Starter Kit preview installed, there's a neat trick:
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:
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:
You need to write the XML representation (property
.OuterXml
) of theXmlNode
to a stream (here aMemoryStream
) and then use theXmlSerializer
to serialize back the object from that stream.You can do it with the generic method and call
or you could even turn that code into either an extension method on the
XmlNode
class so you could write:Marc
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.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
new XmlNodeReader(xmlNode)
to the Deserialize method, you will get the objects populated in the class object just like I populatedsyso
object with XML values.Happy Coding :)
The easiest way to do this would be to use the built in System.Xml.Serialization.XmlSerializer class in .NET. A google search on XmlSerializer will provide a ton of tutorials that you can use to find a tutorial that works for you.