Friday, November 23, 2007

XPath and XmlDocument





Lets look at above example
If you want to select all of the price elements,
here is the code
/* Use This Code to read nodes Element and element value*/
using System.Xml;
using System.Xml.XPath;
....
string fileName = "data.xml";
XPathDocument doc = new XPathDocument(fileName);
XPathNavigator nav = doc.CreateNavigator();

// Compile a standard XPath expression
XPathExpression expr;
expr = nav.Compile("/catalog/cd/price");
XPathNodeIterator iterator = nav.Select(expr);

// Iterate on the node set
listBox1.Items.Clear();
try
{
while (iterator.MoveNext())
{
XPathNavigator nav2 = iterator.Current.Clone();
listBox1.Items.Add("price: " + nav2.Value);
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}


In the above code "/catalog/cd/price" to select all the price elements.

If you just want to select all the cd elements with price greater than 10.0, you can use "/catalog/cd[price>10.0]"


. Here are some more examples of XPath expressions:

1- /catalog selects the root element
2-/catalog/cd selects all the cd elements of the catalog element
3-/catalog/cd/price selects all the price elements of all the cd elements of the catalog element
4-/catalog/cd[price>10.0] selects all the cd elements with price greater than 10.0
5-starts with a slash(/) represents an absolute path to an element
6-starts with two slashes(//) selects all elements that satisfy the criteria
7-//cd selects all cd elements in the document
8- /catalog/cd/title /catalog/cd/artist selects all the title and artist elements of the cd elements of catalog
9- //title //artist selects all the title and artist elements in the document
10-/catalog/cd/* selects all the child elements of all cd elements of the catalog element
11- /catalog/*/price selects all the price elements that are grandchildren of catalog
12- /*/*/price selects all price elements which have two ancestors
13-//* selects all elements in the document
14-/catalog/cd[1]selects the first cd child of catalog
15- /catalog/cd[last()] selects the last cd child of catalog
16- /catalog/cd[price] selects all the cd elements that have price
17-/catalog/cd[price=10.90] selects cd elements with the price of 10.90
18- /catalog/cd[price=10.90]/price selects all price elements with the price of 10.90
19- //@country selects all "country" attributes
20- //cd[@country] selects cd elements which have a "country" attribute
21- //cd[@*] selects cd elements which have any attribute
22- //cd[@country='UK']selects cd elements with "country" attribute equal to 'UK'

To update a cd node, first you have to decide that which node you
are updating by Node.SelectSingleNode("XPathExpression"), and then create a new cd element. After setting the InnerXml of the new node, call ReplaceChild method of XmlElement to update
the document. The code is as follows:

XmlTextReader reader = new XmlTextReader(FILE_NAME);
XmlDocument doc = new XmlDocument();
doc.Load(reader);
reader.Close();

//Select the cd node with the matching title
XmlNode oldCd;
XmlElement root = doc.DocumentElement;
oldCd = root.SelectSingleNode("/catalog/cd[title='" + oldTitle + "']");

XmlElement newCd = doc.CreateElement("cd");
newCd.SetAttribute("country",country.Text);

" + this.comboBox1.Text + "newCd.InnerXml = "" +
"" + artist.Text + "" +
"" + price.Text + "";

root.ReplaceChild(newCd, oldCd);

//save the output to a file
doc.Save(FILE_NAME);


I Hope You will enjoy with this code :-)

No comments: