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
- <Data>
- <Student Name="Saravanan">
-
-
- <Address>Address1</Address>
- <SurName> SurName1
- </ SurName>
- <City>City1</City>
- <State>State1</State>
- </Student>
- <Student Name="Mohan">
- <SurName> SurName2
- </ SurName>
- <Address>Address2</Address>
- <City>City2</City>
- <State>State2</State>
- </Student>
- <Student Name="Fayaz">
- <SurName> SurName3
- </ SurName>
- <Address>Address3</Address>
- <City>City3</City>
- <State>State3</State>
- </Student>
- <Student Name="Siva">
- <SurName> SurName4
- </ SurName>
- <Address>Address4</Address>
- <City>City4</City>
- <State>State4</State>
- </Student>
- <Student Name="Saravanan">
-
-
- <Address>Address5</Address>
- <SurName> SurName5
- </ SurName>
- <City>City5</City>
- <State>State5</State>
- </Student>
- </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;
- XMLDocument StudentDocument = new xmlDocument();
- 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.
- XmlNode Singlenode=StudentDocument.SelectSingleNode("XPath here E.g /Data/Students/Student[@ Name='Mohan']/Address ");
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.
- 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".
- <Student Name="Saravanan">
- <Address>Address1</Address>
- <SurName> SurName1</ SurName>
- <City>City1</City>
- <State>State1</State>
- </Student>
- <Student Name="Saravanan">
- <Address>Address5</Address>
- <SurName> SurName5</ SurName>
- <City>City5</City>
- <State>State5</State>
- </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.
- 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".
- if (Lists.count > 1) {
-
- foreach(XmlNode List in Lists) {
-
- SingleNodeSurName = StudentDocument.SelectSingleNode("XPath here E.g /Data/Students/Student[@ Name='Mohan']/SurName ").InnerText;
-
- if (SingleNodeSurName.toUpper() == UserInputSurName.toUpper()) {
-
- Address,
- City,
- State
- }
- }
- }
I hope this article is very useful. Thank You.