How to read values ​​from an XML file

I have an XML document RESTORE.XML It saves 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 save it in 3 different variables.

How to do this using C #? Thanks.

+4
source share
3 answers

This should work:

 using System.Xml.Linq; XDocument xdoc = XDocument.Load("RESTORE.XML"); xdoc.Descendants("EmployeeID").First().Value; xdoc.Descendants("EmployeeName").First().Value; 
+3
source

try the following:

  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 instead, replace the syntax by loading your xml file ...

+1
source
 XmlDocument doc = new XmlDocument(); doc.Load("restore.xml" ); foreach (XmlNode nd in doc.DocumentElement.SelectNodes( "xml/entry" )) { ... } 
0
source

Source: https://habr.com/ru/post/1339956/


All Articles