first covert your string to xml using XMLDocument LoadXML method
and You can use XNodeList to read the values from XML
Try with below code
Ensure that you have added below space to your page before using below code
usingSystem.Xml;
//Create XMLDocument Object
XmlDocument xml = new XmlDocument();
//Convert your String to XMl
xml.LoadXml(@"<DataChange>
<Commands>
<Command>
<Tables>
<Table name='EMPLOYEE'>
<Fields>
<Field name='ID' recordIdentifier='True'>61</Field>
<Field name='EmpID' recordIdentifier='True'>34</Field>
<Field name='BirthPlace'>US</Field>
</Fields>
</Table>
</Tables>
</Command>
</Commands>
</DataChange>");
//Read the values into XML Node list
XmlNodeList xnList = xml.SelectNodes("/DataChange/Commands/Command/Tables/Table/Fields");
foreach (XmlNode xn in xnList)
{
//Get the ID Element Value
var ID = xn.ChildNodes[0].InnerText;
}
one last query. Is it possible to overwrite xml data at the path Commands/Command/Tables/Table/Fields/Field[@name = 'EmplId'] and create a new xml file ?
Member
15 Points
62 Posts
Read XML in C#
Dec 20, 2013 12:42 AM|psm_8210|LINK
Hello All
I am new to XML. I have a task where I need to get the value of EmpID using either XML query or Linq in C#. Could anyone help on this.
<DataChange>
<Commands>
<Command>
<Tables>
<Table name="EMPLOYEE">
<Fields>
<Field name="ID" recordIdentifier="True">61</Field>
<Field name="EmpID" recordIdentifier="True">34</Field>
<Field name="BirthPlace">US</Field>
</Fields>
</Table>
</Tables>
</Command>
</Commands>
</DataChange>
All-Star
50831 Points
9895 Posts
Re: Read XML in C#
Dec 20, 2013 12:52 AM|A2H|LINK
first covert your string to xml using XMLDocument LoadXML method and You can use XNodeList to read the values from XML
Try with below code
Ensure that you have added below space to your page before using below code
Aje
My Blog | Dotnet Funda
All-Star
31362 Points
7055 Posts
Re: Read XML in C#
Dec 20, 2013 12:56 AM|kaushalparik27|LINK
You can accomplish that with a single line of code with XPath as:
You will require to add this namespace:
And if you have multiple tables in the xml then you can add that condition as well for table name as:
It is important to note here that Xml is Case Sensitive.. so, it will require to write the xpath in exact case that we have in xml
hope it helps./.
[KaushaL] Blog Twitter [MS MVP 2008 & 2009] [MCC 2011] [MVP Reconnect 2017]
Don't forget to click "Mark as Answer" on the post that helped you
Member
15 Points
62 Posts
Re: Read XML in C#
Dec 20, 2013 01:09 AM|psm_8210|LINK
Thanks Kaushal,
One question. the XML content is in the XML file. Do I need to load the XML into the string (strXML). What is XML is huge.
All-Star
31362 Points
7055 Posts
Re: Read XML in C#
Dec 20, 2013 01:20 AM|kaushalparik27|LINK
You can definately read the XML from XML file. If the XML is huge then it should be in file only. Here is the way you can read it from XML file:
hope it helps./.
[KaushaL] Blog Twitter [MS MVP 2008 & 2009] [MCC 2011] [MVP Reconnect 2017]
Don't forget to click "Mark as Answer" on the post that helped you
Member
15 Points
62 Posts
Re: Read XML in C#
Dec 20, 2013 01:52 AM|psm_8210|LINK
Thanks Kaushal,
one last query. Is it possible to overwrite xml data at the path Commands/Command/Tables/Table/Fields/Field[@name = 'EmplId'] and create a new xml file ?
All-Star
31362 Points
7055 Posts
Re: Read XML in C#
Dec 20, 2013 01:57 AM|kaushalparik27|LINK
Sure. Below is example where I have changed the value of EmpID and writing it back/overwriting to xml file:
hope it helps./.
[KaushaL] Blog Twitter [MS MVP 2008 & 2009] [MCC 2011] [MVP Reconnect 2017]
Don't forget to click "Mark as Answer" on the post that helped you
Member
15 Points
62 Posts
Re: Read XML in C#
Dec 20, 2013 02:05 AM|psm_8210|LINK
Many Thanks,
Everthing seems to be working as you said. I will do the analysis of your solution and make the code changes accordingly. :)