Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Read values from XML file

Tags:

c#

I have a XML document called RESTORE.XML It hold these values..

<EmployeeDetails>
  <EmployeeID>156824</EmployeeID>
  <EmployeeName>ALEX</EmployeeName>
  <EmployeeAge>29</EmployeeAge>
</EmployeeDetails>

From my c# application i want to read these three values and store it in 3 different variables.

How can do it using c# ? Thanks.

like image 543
Anuya Avatar asked Feb 16 '11 08:02

Anuya


2 Answers

This should work:

using System.Xml.Linq;
XDocument xdoc = XDocument.Load("RESTORE.XML");
xdoc.Descendants("EmployeeID").First().Value;
xdoc.Descendants("EmployeeName").First().Value;
like image 193
digg Avatar answered Oct 12 '22 20:10

digg


try this:

            XElement xml = XElement.Parse(@"
<EmployeeDetails>
  <EmployeeID>156824</EmployeeID>
  <EmployeeName>ALEX</EmployeeName>
  <EmployeeAge>29</EmployeeAge>
</EmployeeDetails>");

            int EmployeeID = int.Parse(xml.Element("EmployeeID").Value);
            string EmployeeName = xml.Element("EmployeeName").Value;
            int EmployeeAge = int.Parse(xml.Element("EmployeeAge").Value);

but replace the parse with a load of your xml file instead...

like image 44
Johan Leino Avatar answered Oct 12 '22 19:10

Johan Leino