Content of sample ARTICLE.XML file:
<ARTICLES>
<ARTICLE>
<TITLE>Developers Guide To XML</TITLE>
<AUTHOR id='101' location='St Louis'>Manish Sharma</AUTHOR>
<AUTHOR id='102' location='Chicago'>Steven Berger</AUTHOR>
</ARTICLE>
</ARTICLES>
Load xml file in XmlDocument object:
<% @Page language="C#" debug="true" %>
<%@ Import Namespace="System.Xml" %>
<SCRIPT runat='server'>
void Page_Load(Object Sender,EventArgs e)
{
XmlDocument xd = new XmlDocument();
xd.Load(Server.MapPath("ARTICLES.xml"));
Response.Write (xd.OuterXml);
xd = null;
}
</SCRIPT>
Traverse through child nodes using enumerable ChildNodes object:
<% @Page language="C#" debug="true" %>
<%@ Import Namespace="System.Xml" %>
<SCRIPT runat='server'>
void Page_Load(Object Sender,EventArgs e)
{
XmlDocument xd = new XmlDocument();
XmlNode ndARTICLE;
xd.Load(Server.MapPath("ARTICLES.xml"));
ndARTICLE = xd.FirstChild["ARTICLE"];
foreach(XmlNode nd in ndARTICLE.ChildNodes)
{
if(nd.Name == "AUTHOR")
Response.Write("The author's name is " + nd.InnerText + "<BR>");
}
}
</SCRIPT>
Read Xml file using the XmlTextReader Object:
<% @Page language="C#" debug="true" %>
<%@ Import Namespace="System.Xml" %>
<SCRIPT runat='server'>
void Page_Load(Object Sender,EventArgs e)
{
XmlTextReader xr = new XmlTextReader(Server.MapPath("ARTICLES.xml"));
Boolean bTitle = false;
while(xr.Read())
{
switch(xr.NodeType)
{
case XmlNodeType.Element:
if(xr.Name == "TITLE")
bTitle = true;
break;
case XmlNodeType.Text:
if(bTitle)
{
Response.Write("ARTICLE title: " + xr.ReadString());
bTitle = false;
}
break;
}
}
}
</SCRIPT>
Write to XML file using the XmlTextWriter Object:
<%@ Import Namespace="System.Xml" %>
<SCRIPT runat='server'>
void Page_Load(Object Sender,EventArgs e)
{
XmlTextWriter xw = new XmlTextWriter(Server.MapPath("ARTICLES2.xml"),
Encoding.UTF8);
try
{
xw.WriteStartDocument();
xw.WriteStartElement("ARTICLE");
xw.WriteElementString("TITLE", "How To Use XmlTextWriter");
xw.WriteEndDocument();
Response.Write("Your file has been written.");
}
catch(Exception ex)
{
Response.Write("Exception: " + ex.Message);
}
finally
{
xw.Flush();
xw.Close();
}
}
</SCRIPT>
Navigate through the Xml file using the XmlNodeReader Object:
<%@ Import Namespace="System.Xml" %>
<SCRIPT runat='server'>
void Page_Load(Object Sender,EventArgs e)
{
XmlDocument xd = new XmlDocument();
xd.Load(Server.MapPath("ARTICLES.xml"));
XmlNodeReader xn = new XmlNodeReader(xd);
while(xn.Read())
{
Response.Write(xn.Name + " - " + xn.Value + "<BR>");
}
}
</SCRIPT>
Retrieve a subset of Nodes using XPathNavigator object:
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.XPath" %>
<SCRIPT runat='server'>
void Page_Load(Object Sender,EventArgs e)
{
XPathDocument xpd = new XPathDocument(Server.MapPath("ARTICLES.xml"));
XPathNavigator nav = xpd.CreateNavigator();
XPathNodeIterator iterator = nav.Select("ARTICLES/ARTICLE/AUTHOR");
while(iterator.MoveNext())
{
Response.Write(iterator.Current.Value + "<BR>");
}
}
</SCRIPT>
Extract the Attribute values of a Node using XPathNavigator:
<%@ Page debug="true" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.XPath" %>
<SCRIPT runat='server'>
void Page_Load(Object Sender,EventArgs e)
{
XPathDocument xpd = new XPathDocument(Server.MapPath("ARTICLES.xml"));
XPathNavigator nav = xpd.CreateNavigator();
XPathNodeIterator iterator = nav.Select("ARTICLES/ARTICLE/AUTHOR");
while(iterator.MoveNext())
{
Response.Write(iterator.Current);
Response.Write(" ID: " + iterator.Current.GetAttribute("id", "") + "<BR>");
}
}
</SCRIPT>
Insert a new item into the Xml document using the InsertAfter method:
<%@ Page debug="true" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.XPath" %>
<SCRIPT runat='server'>
void Page_Load(Object Sender,EventArgs e)
{
XmlDocument xd = new XmlDocument();
xd.Load(Server.MapPath("ARTICLES.xml"));
XmlNode root = xd.DocumentElement; // ARTICLES
// Insert ARTICLE element
XmlElement elemARTICLE = xd.CreateElement("ARTICLE");
root.InsertAfter(elemARTICLE, root.FirstChild);
xd.Save(Server.MapPath("output.xml"));
Response.Write("Open the file output.xml to view the results.");
}
</SCRIPT>
Insert a new child node using the AppendChild method:
<%@ Page debug="true" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.XPath" %>
<SCRIPT runat='server'>
void Page_Load(Object Sender,EventArgs e)
{
XmlDocument xd = new XmlDocument();
xd.Load(Server.MapPath("ARTICLES.xml"));
XmlNode root = xd.DocumentElement; // ARTICLES
// Insert ARTICLE element
XmlElement elemARTICLE = xd.CreateElement("ARTICLE");
root.InsertAfter(elemARTICLE, root.FirstChild);
// Insert TITLE element beneath the ARTICLE
XmlElement elemTitle = xd.CreateElement("TITLE");
elemTitle.InnerText = "MY TITLE";
elemARTICLE.AppendChild(elemTitle);
xd.Save(Server.MapPath("output.xml"));
Response.Write("Open the file output.xml to view the results.");
}
</SCRIPT>
Query XML file using XPath Expressions:
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.XPath" %>
<SCRIPT runat='server'>
private void btnQuery_Click(System.Object sender, System.EventArgs e)
{
XmlDocument xd = new XmlDocument();
XmlNodeList nl;
xd.Load(Server.MapPath("ARTICLES.xml"));
nl = xd.SelectNodes("ARTICLES/ARTICLE/AUTHOR");
string output = string.empty;
foreach(XmlNode nd in nl)
{
output += nd.OuterXml;
}
Reponse.Write(output);
}
</SCRIPT>
Output:
<ARTICLES>
<ARTICLE>
<TITLE>Developers Guide To XML</TITLE>
<AUTHOR id="101" location="St Louis">Manish Sharma</AUTHOR>
<AUTHOR id="102" location="Chicago">Steven Berger</AUTHOR>
</ARTICLE>
</ARTICLES>
Retrieve an Author with a particular ID using XPath:
XPath Query Expression: /ARTICLES/ARTICLE/AUTHOR[@id = "102"]
Output: <AUTHOR id="102" location="Chicago">Steven Berger</AUTHOR>
Retrieve multiple instances of the same Author using XPath:
Sample XML file:
<ARTICLES>
<ARTICLE>
<TITLE>Developers Guide To XML</TITLE>
<AUTHOR id="101" location="St Louis">Manish Sharma</AUTHOR>
<AUTHOR id="102" location="Chicago">Steven Berger</AUTHOR>
</ARTICLE>
<ARTICLE>
<TITLE>How to cook chinese food</TITLE>
<AUTHOR id="102" location="Chicago">Steven Berger</AUTHOR>
</ARTICLE>
<ARTICLE>
<TITLE>How to lose weight fast</TITLE>
<AUTHOR id="102" location="Chicago">Steven Berger</AUTHOR>
</ARTICLE>
</ARTICLES>
XPath Query Expression: /ARTICLES/ARTICLE/AUTHOR[@id = "102"]/parent::*
Output:
<ARTICLE>
<TITLE>Developers Guide To XML</TITLE>
<AUTHOR id="101" location="St Louis">Manish Sharma</AUTHOR>
<AUTHOR id="102" location="Chicago">Steven Berger</AUTHOR>
</ARTICLE>
<ARTICLE>
<TITLE>How to cook chinese food</TITLE>
<AUTHOR id="102" location="Chicago">Steven Berger</AUTHOR>
</ARTICLE>
<ARTICLE>
<TITLE>How to lose weight fast</TITLE>
<AUTHOR id="102" location="Chicago">Steven Berger</AUTHOR>
</ARTICLE>
The parent::* clause added to the end of the XPath expression in this example tells the query to retrieve the data as well as the parent node. Rather than just returning the same author three times, as it would if we'd specified only /ARTICLES/ARTICLE/ AUTHOR[@id = "102"], we instead get the author node and the ARTICLE parent node, which makes more sense.
Use OR in an XPath to Query based on multiple search criteria:
XPath Query Expression: ARTICLES/ARTICLE/AUTHOR[@location="Chicago" or @location="St Louis"]
Output:
<AUTHOR id="101" location="St Louis">Manish Sharma</AUTHOR>
<AUTHOR id="102" location="Chicago">Steven Berger</AUTHOR>
<AUTHOR id="102" location="Chicago">Steven Berger</AUTHOR>
<AUTHOR id="102" location="Chicago">Steven Berger</AUTHOR>
Use XPath to exclude Nodes based on the value of Attributes:
XPath Query Expression: /ARTICLES/ARTICLE/AUTHOR[@location != "Chicago"]
Output: <AUTHOR id="101" location="St Louis">Manish Sharma</AUTHOR>
You could also perform greater-than and less-than comparisons using this technique. The XPath expression /ARTICLES/ARTICLE/AUTHOR[@id > 105] retrieves all authors whose id attribute is greater than 105.
Retrieve a specific TITLE Node by querying on its text value:
XPath Query Expression: /ARTICLES/ARTICLE/TITLE[. = "How to cook chinese food"]/parent::*
Output:
<ARTICLE>
<TITLE>How to cook chinese food</TITLE>
<AUTHOR id="102" location="Chicago">Steven Berger</AUTHOR>
</ARTICLE>
The dot (.) operator is used to indicate "right here."
Retrieve a specific ARTICLE Node by querying on the text found in the ARTICLE's TITLE Node:
XPath Query Expression: /ARTICLES/ARTICLE[TITLE = 'How to cook chinese food']
Output:
<ARTICLE>
<TITLE>How to cook chinese food</TITLE>
<AUTHOR id="102" location="Chicago">Steven Berger</AUTHOR>
</ARTICLE>