Parsing data from an XML string property - C#
Sure this is a simple one, but I often find myself coming back to working with Xml strings and having to remind myself exactly how to extract the data I need. Why not make life easier and make a reference for next time.
So, suppose you have c# object 'user', which has a 'Data' string property that contains Xml that you would like to query.
e.g. user.Data == "<root><name>This is the data</name></root>
XmlDocument doc = new XmlDocument();
doc.LoadXml(user.Data);
var result = doc.SelectSingleNode("//name");</code></pre>
Line 1: Instantiate your 'XmlDocument' type
Line 2: Load the xml passing in a string argument (or filepath to an xmldoc)
Line 3: Provide your xPath to the data you want to extract
Query away....
Written by Glenn Spinks
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Xml
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#