Accessing XML Data From XML Document In C# Console Application

In this article, I am going to give you the best way to access XML data from an XML document and also how to find the common record node count.

Students.xml

  1. <Data>  
  2.     <Student Name="Saravanan"// Node with same name  
  3.   
  4.   
  5.         <Address>Address1</Address>  
  6.         <SurName> SurName1  
  7.         </ SurName>  
  8.         <City>City1</City>  
  9.         <State>State1</State>  
  10.     </Student>  
  11.     <Student Name="Mohan">  
  12.         <SurName> SurName2  
  13.         </ SurName>  
  14.         <Address>Address2</Address>  
  15.         <City>City2</City>  
  16.         <State>State2</State>  
  17.     </Student>  
  18.     <Student Name="Fayaz">  
  19.         <SurName> SurName3  
  20.         </ SurName>  
  21.         <Address>Address3</Address>  
  22.         <City>City3</City>  
  23.         <State>State3</State>  
  24.     </Student>  
  25.     <Student Name="Siva">  
  26.         <SurName> SurName4  
  27.         </ SurName>  
  28.         <Address>Address4</Address>  
  29.         <City>City4</City>  
  30.         <State>State4</State>  
  31.     </Student>  
  32.     <Student Name="Saravanan"// Node with same name  
  33.   
  34.   
  35.         <Address>Address5</Address>  
  36.         <SurName> SurName5  
  37.         </ SurName>  
  38.         <City>City5</City>  
  39.         <State>State5</State>  
  40.     </Student>  
  41. </Data>  

Step 1

Before we do anything with the XML document, we have to first load the XML file. The following code is the common way to load a XML document.

Note

Before dealing with XML, initilize the XML Namespace, i.e., using system.xml;

  1. XMLDocument  StudentDocument = new xmlDocument();  
  2. studentDocument("give the XML Documnet Path here e.g C://Studentrecords/Student.xml");  

Step 2

Now, we are going to access the XML data from XML document by using XPath.

XPath

XPath is used to navigate through elements and attributes in an XML document. In other words, XPath uses the path expressions to select nodes or node-sets in an XML Document.

Here is the code:

By using XmlNode keyword, we can access the node value.

  1. XmlNode Singlenode=StudentDocument.SelectSingleNode("XPath here E.g /Data/Students/Student[@ Name='Mohan']/Address "); // accessing one student record  

Note

Still, the data is in XML format. So we assign the node value to another string variable so that the value can be used for further purpose.

  1. string StudentAddress= Singlenode.InnerText;  

Now, consider the situation of needing to get the correct data from XML document if two students have the same name. Then, by comparing the surname, we can get the correct data. Try the following code.

Note

Consider the above XML document. It has two nodes with the same name "Saravanan".

  1. <Student Name="Saravanan"// Node with same name  
  2.     <Address>Address1</Address>  
  3.     <SurName> SurName1</ SurName>  
  4.         <City>City1</City>  
  5.         <State>State1</State>  
  6. </Student>  
  7. <Student Name="Saravanan"// Node with same name  
  8.     <Address>Address5</Address>  
  9.     <SurName> SurName5</ SurName>  
  10.         <City>City5</City>  
  11.         <State>State5</State>  
  12. </Student>  

Eg

User input is---- Student Name - Saravanan and SurName-SurName5

To find the number of nodes with the same value, try the following code.

  1. XmlNodeList Lists = Students.SelectNodes(" /Data/Students/Student[@ Name='Saravanan']/Address ");  

Note: For getting a single node value, we use "XmlNode" Keyword, but for finding more than one nodes with the same value, we have to use "XmlNodeList".

  1. if (Lists.count > 1) {  
  2.     //if this loop is executes ,mean there is morethan one node with same value  
  3.     foreach(XmlNode List in Lists) {  
  4.         //this loop is for comparing the surname to get correct node value  
  5.         SingleNodeSurName = StudentDocument.SelectSingleNode("XPath here E.g /Data/Students/Student[@ Name='Mohan']/SurName ").InnerText;  
  6.         //Now compare the Address with user input address  
  7.         if (SingleNodeSurName.toUpper() == UserInputSurName.toUpper()) {  
  8.             //if two Surname is same it get all other data's by the same above method  
  9.             Address,  
  10.             City,  
  11.             State  
  12.         }  
  13.     }  
  14. }  

I hope this article is very useful. Thank You.


Similar Articles