V.V V.V - 2 months ago 6
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#.

<structure>
<a>Test Name</a>
<e>test address</e>
<c>
<c1>yyyy<c1>
<c2>xxxx</c2>
</c>
</structure>


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();
xmldoc.LoadXml(abcd);

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#?

L.B L.B
Answer

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;
Comments