Basic XML Operations in C#

Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications , all gratis open standards.

Here I am explaining you the basic XML operations which is normally used in the  C# coding .


Select XML Nodes by Name [C#]

To find nodes in an XML file you can use XPath expressions. Method XmlNode.Selec­tNodes returns a list of nodes selected by the XPath string. Method XmlNode.Selec­tSingleNode finds the first node that matches the XPath string.
Suppose we have this XML file.
<Names>

    <Name>

        <FirstName>Vishal</FirstName>

        <LastName>Soni</LastName>

    </Name>

    <Name>

        <FirstName>Bharat</FirstName>

        <LastName> Shekhar</LastName>

    </Name>

</Names>
To get all <Name> nodes use XPath expression /Names/Name. The first slash means that the <Names> node must be a root node. SelectNodes method returns collection XmlNodeList which will contain the <Name> nodes. To get value of sub node <FirstName> you can simply index XmlNode with the node name: xmlNode["FirstName"].InnerText. See the example below.

@[C#]
XmlDocument xml = new XmlDocument();

xml.LoadXml(myXmlString); // suppose that myXmlString contains "<Names>...</Names>"



XmlNodeList xnList = xml.SelectNodes("/Names/Name");

foreach (XmlNode xn in xnList)

{

  string firstName = xn["FirstName"].InnerText;

  string lastName = xn["LastName"].InnerText;

  Console.WriteLine("Name: {0} {1}", firstName, lastName);

}


The output is:
Name: Vishal Soni
Name: Bharat Shekhar

Select XML Nodes by Attribute Value [C#]

This example shows how to select nodes from XML document by attribute value. Use method XmlNode.Selec­tNodes to get list of nodes selected by the XPath expression. Suppose we have this XML file.
[XML]

<Names>

    <Name type="M"> Bharat</Name>

    <Name type="F"> Shekhar</Name>

    <Name type="M"> Vashistha</Name>

</Names>

To get all name nodes use XPath expression /Names/Name. To get only male names (to select all nodes with specific XML attribute) use XPath expression /Names/Name[@type='M'].
@[C#]
XmlDocument xml = new XmlDocument();

xml.LoadXml(str);  // suppose that str string contains "<Names>...</Names>"



XmlNodeList xnList = xml.SelectNodes("/Names/Name[@type='M']");

foreach (XmlNode xn in xnList)

{

  Console.WriteLine(xn.InnerText);

}


The output is:
Bharat
Vashistha

Select Top XML Nodes using XPath [C#]

This example shows how to select Top N nodes of the specific name from an XML document. To select nodes from XML use method XmlNode.Selec­tNodes. Pass XPath expression as a parameter and the method returns a list of selected nodes. Suppose we have this XML file.
[XML]
<Names>

    <Name>Vishal</Name>

    <Name>Piyush</Name>

    <Name>Shiv</Name>

    <Name>Amit</Name>

    <Name>Bharat</Name>

    <Name>Shekhar</Name>

    <Name>Vashistha</Name>

</Names>
To get all <Name> nodes use XPath expression /Names/Name. If you don't want to selected all nodes, but only top 5 nodes, you can uses XPath expression like this /Names/Name[position() <= 5]. See the example below.
[C#]
XmlDocument xml = new XmlDocument();

xml.LoadXml(str);  // suppose that str string contains "<Names>...</Names>"



XmlNodeList xnList = xml.SelectNodes("/Names/Name[position() <= 5]");

foreach (XmlNode xn in xnList)

{

  Console.WriteLine(xn.InnerText);

}


The output is:
Vishal
Piyush
Shiv
Amit
Bharat

 Hope this will help you ..

Regards ,
Rajesh







1 comment: