|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionIn this article I will try to introduce XPath Data Model and the way we can navigate through xml document, manipulate it and run expressions against it. 1- Creating XPathDocument and XPathNavigator Object Before we proceed, lets first create a small xml file - EE.xml and place it in App_Code folder of our web solution. <?xml version="1.0" standalone="yes"?>
<PlotsDataSet>
<Plot>
<PlotStatus>
<PlotID>4</PlotID>
<PlotStatus>In Active</PlotStatus>
</PlotStatus>
<PlotDescription>
<PlotName>3 Shrewsbury Road</PlotName>
<PlotSize>30 Sq. Yards</PlotSize>
</PlotDescription>
<PlotPrice>50000</PlotPrice>
</Plot>
</PlotsDataSet>
using System.Xml;
using System.Xml.XPath;
Thirdly, add a TextBox on form and make it multiline. 1- Creating XPathDocument and XPathNavigator ObjectSimply create an instantiation of XPathDocument class and populate the object with xml data from the file that we have stored in App_Code. Then we will call the CreateNavigator method on XPathDocument object which thus gives us the XPathNavigator Object. Thats how we will do this: XPathDocument doc = new XPathDocument(Server.MapPath("App_Code//EE.xml"));
XPathNavigator nav = doc.CreateNavigator();
2- Selecting XML Data using XPathNodeIteratorXPathNodeIterator lets us get through all the nodes, lets say I want to get PlotName node information in all Plot Elements. We can achieve this by creating XPathNodeIterator object and using its MoveNext() method. XPathNodeIterator xnodes = nav.Select("/PlotsDataSet/Plot/PlotDescription/PlotName");
TextBox1.Text += "Plot Names:" + "\n";
while (xnodes.MoveNext())
{
TextBox1.Text += "\n"+ xnodes.Current.InnerXml;
}
3- Inserting XML DataOnce we have added an XML Navigator we can call any of its methods to manipulate the XML within XMLDocument. For example we have methods to insert elements, append/prepend child elements. XML Navigator object has several methods with the help of which we can navigate an XML Document and change or add information that we want to. Lets insert XPathNavigator nav2 = doc.CreateNavigator();
nav2.MoveToChild("PlotsDataSet", string.Empty);
nav2.MoveToChild("Plot", string.Empty);
nav2.MoveToChild("PlotPrice", string.Empty);
nav2.InsertAfter("
If we now look at the XML document, its changed with Discount element added right after PlotPrice Element. <?xml version="1.0" standalone="yes"?>
<PlotsDataSet>
<Plot>
<PlotStatus>
<PlotID>4</PlotID>
<PlotStatus>In Active</PlotStatus>
</PlotStatus>
<PlotDescription>
<PlotName>3 Shrewsbury Road</PlotName>
<PlotSize>30 Sq. Yards</PlotSize>
</PlotDescription>
<PlotPrice>50000</PlotPrice>
<Discount>Yes - 15 Percent</Discount>
</Plot>
</PlotsDataSet>
4- Evaluating XML Data using XPathExpressionWith XPath Navigator we also have capability to evaluate expression within our xpath model. To achieve this we must have XML Document or XPath Document. Then we can create our XPath Navigator based on either one of them. Once we have xpath navigator, we can simply call its evaluate method and pass in the expression that we like to evaluate. XPathNavigator nav3 = doc.CreateNavigator();
XPathExpression xpr = nav3.Compile("/PlotsDataSet/Plot/PlotPrice/text() * 0.15");
Double DiscountedPrice = Convert.ToDouble(nav3.Evaluate(xpr));
TextBox1.Text += "\n" + "Total Discount: "+ DiscountedPrice.ToString();
I hope you have enjoyed the article. If you come across any questions, do let me know.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||