boutique replica bags up ideas

the best replique rolex and prices here.

julia highlight 99j hair color 10a quality straight human hair lace front wigs 8 - 24 inches pre plucked hairline 13x4 inches lace front brazilian wig onlinefor sale

C# – Get Specified Node values from XML Document

Updated on     Kisan Patel

An XML document consists of a collection of nodes and attributes of the nodes. You can modify the data of an XML document by modifying the nodes or attributes. The System.Xml.XmlDocument class is used to modify the data of XML documents, by performing various tasks, such as inserting, deleting, or updating the data in the XML documents.

Here, we can use XmlDatadocument, XmlDocument, XmlElement, XmlAttribute, XmlNode and XmlNodeList classes to implement XML DOM model. The XmlNode class is an abstract class that represents an XML document; while the XmlNodeList class represents an oredered list of nodes. This classes belong to the System.Xml namespace.

How to Find Element by Tag Name in XML

You need to follow below students.xml file to understand this tutorial.

<?xml version="1.0" encoding="utf-8" ?>
<students>
   <student id="1">
      <name>Kisan</name>
      <city country="India">Ladol</city>
   </student>
   <student id="2">
      <name>Ravi</name>
      <city country="US">Vijapur</city>
   </student>
   <student id="3">
      <name>Ujas</name>
      <city country="India">Ladol</city>
   </student>
   <student id="4">
      <name>Ketul</name>
      <city country="England">Ladol</city>
   </student>
</students>

Now, you can use below line of code to retrieve elements from an XML document by using their tag names.

XmlDocument xd = new XmlDocument();
xd.Load("D:\\students.xml");
XmlNodeList nl = xd.GetElementsByTagName("name");
foreach (XmlNode node in nl) {
    Console.WriteLine(node.InnerText);
}

…output of the above C# code…

GetElementsByTagName-example

In above code, we have loaded the XML file, students.xml in the XmlDocument object from the disk. Next, we have retrieved all the elements with the tag name “name” by calling the GetElementsByTagName() method of the XmlDocument object and added them in a collection, which is an object of the XmlNodeList class.

Now, If you want to get single node by node value then use below line of C# code.

XmlDocument xd = new XmlDocument();
xd.Load("D:\\students.xml");
//Create XPath
string str = "students/student[name='Ravi']";
XmlNode foundNode = xd.SelectSingleNode(str);
if (foundNode != null) {
   Console.WriteLine(foundNode.OuterXml);
}

…output of the above C# code…

SelectSingleNode-example

In above code, we have taken a reference variable of the XmlNode class and passed the XPath string to the SelectSingleNode() method of the XmlDocument object, and assigned the return value to the XmlNode object. In above code, the SelectSignleNode() method returns the “name” element of the student element node. Then, we have displayed OuterXml of the node in a console.


C#

Leave a Reply