V.V V.V - 1 year ago 58
C# Question

read the data from XML Structure using c#

I store the xml file in one string object like,I stored the xml structure in local variable string abcd in c#.

<a>Test Name</a>
<e>test address</e>

How to read(parse) this xml string using c# and store the tag a,and tag c1 ,tag c2 values in local variable using c#.

i tried like

XmlDocument xmldoc = new XmlDocument();

XmlElement element = (XmlElement)xmldoc.GetElementById("a");

but i get null value.how to read the values from xml structure and stored in local variable using c#?

Answer Source

Linq2Xml is much easier to use.

var xElem = XElement.Parse(abcd);
var a = xElem.Element("a").Value;
var c = xElem.Element("c").Element("c1").Value;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download