Last post Dec 02, 2010 08:19 PM by decker dong - msft
Dec 01, 2010 11:36 PM|nwyork|LINK
I'm using c# classes to map to xml returned from a server.
One problem I'm running into is when I have a element that can be the root element or child depending on the xml returned.
How can I deserialize the xml to this class using xml attributes regardless if it is in the root or a child?
Right now I have two classes with different names. One has [XmlRoot("ERROR")] the other has [XmlElement("ERROR")] in it's parent class. This works but I would like to have one c# class for this error element.
I thought of creating a wrapper class that would add the [XmlRoot("ERROR")] attribute depending on how I call it.
I gotta believe there is a better way than both of these approaches.
Dec 02, 2010 08:19 PM|decker dong - msft|LINK
Suppose your Xml looks like this sample:
<?xml version="1.0" encoding="utf-8" ?>
<Error>My Error Here</Error>
XmlDocument doc = new XmlDocument();
XmlNode node = doc.SelectSingleNode("Root/Error");
//Do what you want here.....
//Do what you haven't got