Monday 21 March 2011

Dot.Net: XML Deserialize XElement

Question:
How do I deserialize a given string using XElement in dot.net??



Answer:
You deserialize as you would do without XElement.
The only difference is, that you get the XmlReader directly from the XElement.

Here is an example
If this is the xml

    
      
      
    
  
 

... then you get your friend back this way:
XElement xFriend = XElement.Parse(xmlFriendStr);
   XmlSerializer XSer = new XmlSerializer(typeof(Friend));
   Friend f = XSer.Deserialize(xFriend.CreateReader()) as Friend;

No comments: