Monday 25 July 2011

xPath Document, Navigator, Expression.

Read BookStore.xml using XPath Iterator.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;

namespace BookStoreApp
{
class Program
{
static void Main(string[] args)
{

Program bk = new Program();

bk.DisplayBooksLess10();

bk.DisplayNovel();
bk.DisplayFirstNameAInIt();
bk.DisplayNovelOverSix();

//Add the new book
bk.AddBookNode();
}


public void DisplayFirstNameAInIt()
{
Console.WriteLine("A char in Author's first name");

XPathNavigator nav;
XPathDocument docNav;
XPathNodeIterator NodeIter;

//String strExpression;





// Open the XML.
docNav = new XPathDocument("BookStore.xml");

nav = docNav.CreateNavigator();

XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
nsmgr.AddNamespace("b", "http://www.contoso.com/books");
nsmgr.AddNamespace("g", "http://www.contoso.com/genre");
nsmgr.AddNamespace("a", "http://www.contoso.com/author");

XPathExpression expr = nav.Compile("/b:bookstore/b:book[contains(a:author/a:first-name,'a')]/b:title");
expr.SetContext(nsmgr);


//strExpression = "/bookstore/book/title[../price>10.00]";

NodeIter = nav.Select(expr);

while (NodeIter.MoveNext())
{

Console.WriteLine("Book Title: {0}", NodeIter.Current.Value);
};
Console.WriteLine("\n" + "Press ENTER key....");
Console.ReadLine();


}

public void DisplayNovelOverSix()
{
Console.WriteLine("Average of Novels over £6");

XPathNavigator nav;
XPathDocument docNav;
XPathNodeIterator NodeIter;
//String strExpression;





// Open the XML.
docNav = new XPathDocument("BookStore.xml");

nav = docNav.CreateNavigator();

XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
nsmgr.AddNamespace("b", "http://www.contoso.com/books");
nsmgr.AddNamespace("g", "http://www.contoso.com/genre");
nsmgr.AddNamespace("a", "http://www.contoso.com/author");

//XPathExpression expr = nav.Compile("/b:bookstore/b:book[@g:genre='novel' and b:price>6.00]/b:title");
//expr.SetContext(nsmgr);


string avg = nav.Evaluate("sum(descendant::b:bookstore/b:book[@g:genre='novel' and b:price>6.00]/b:price)" +
" div count(descendant::b:bookstore/b:book[@g:genre='novel' and b:price>6.00])", nsmgr).ToString();




Console.WriteLine("Books Average Price: {0}", avg);
Console.WriteLine("\n" + "Press ENTER key....");
Console.ReadLine();


}

public void DisplayNovel()
{
Console.WriteLine("Display Novels");

XPathNavigator nav;
XPathDocument docNav;
XPathNodeIterator NodeIter;
//String strExpression;





// Open the XML.
docNav = new XPathDocument("BookStore.xml");

nav = docNav.CreateNavigator();

XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
nsmgr.AddNamespace("b", "http://www.contoso.com/books");
nsmgr.AddNamespace("g", "http://www.contoso.com/genre");

XPathExpression expr = nav.Compile("/b:bookstore/b:book[@g:genre='novel']/b:title");
expr.SetContext(nsmgr);


//strExpression = "/bookstore/book/title[../price>10.00]";

NodeIter = nav.Select(expr);

while (NodeIter.MoveNext())
{

Console.WriteLine("Book Title: {0}", NodeIter.Current.Value);
};
Console.WriteLine("\n" + "Press ENTER key....");
Console.ReadLine();


}
public void DisplayBooksLess10()
{
Console.WriteLine("Books less than £10");

XPathNavigator nav;
XPathDocument docNav;
XPathNodeIterator NodeIter;
//String strExpression;





// Open the XML.
docNav = new XPathDocument("BookStore.xml");

nav = docNav.CreateNavigator();

XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
nsmgr.AddNamespace("b", "http://www.contoso.com/books");
nsmgr.AddNamespace("g", "http://www.contoso.com/genre");
nsmgr.AddNamespace("a", "http://www.contoso.com/author");

XPathExpression expr = nav.Compile("/b:bookstore/b:book[b:price<'10.00']/b:title");
expr.SetContext(nsmgr);


//strExpression = "/bookstore/book/title[../price>10.00]";

NodeIter = nav.Select(expr);

while (NodeIter.MoveNext())
{

Console.WriteLine("Book Title: {0}", NodeIter.Current.Value);
};
Console.WriteLine("\n" + "Press ENTER key....");
Console.ReadLine();


}

public void AddBookNode()
{

Console.WriteLine("Adding a book node to existing xml");

XmlDocument myDoc = new XmlDocument();
myDoc.Load("BookStore.xml");
XmlNode root = myDoc.DocumentElement;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(myDoc.NameTable);
nsmgr.AddNamespace("b", "http://www.contoso.com/books");
nsmgr.AddNamespace("g", "http://www.contoso.com/genre");
nsmgr.AddNamespace("a", "http://www.contoso.com/author");
// Create a Book element and populate its attributes
System.Xml.XmlElement newBook = myDoc.CreateElement("book", "http://www.contoso.com/books");
//create the three attributes to hold the values
newBook.SetAttribute("genre", "http://www.contoso.com/genre", "novel");
newBook.SetAttribute("publicationdate", "2011-06-14");
newBook.SetAttribute("ISBN", "1-1245-45455");
System.Xml.XmlElement xeTitle = myDoc.CreateElement("title");
xeTitle.InnerText = "MyBook";
// Insert the new element under the node we created
newBook.AppendChild(xeTitle);
System.Xml.XmlElement myAuthor = myDoc.CreateElement("author");
myAuthor.SetAttribute("xmlns", ("http://www.contoso.com/author"));
System.Xml.XmlElement xeFirstname = myDoc.CreateElement("first-name");
xeFirstname.InnerText = "Siraj";
myAuthor.AppendChild(xeFirstname);
System.Xml.XmlElement xeLastname = myDoc.CreateElement("last-name");
xeLastname.InnerText = "Zarook";
myAuthor.AppendChild(xeLastname);
newBook.AppendChild(myAuthor);
// Price
System.Xml.XmlElement xePrice = myDoc.CreateElement("price");
xePrice.InnerText = "1.47";
// Insert the new element under the node we created
newBook.AppendChild(xePrice);
//append the whole node to file
myDoc.DocumentElement.AppendChild(newBook);
myDoc.Save("BookStoreModified.xml");

Console.WriteLine("File BookStoreModified.xml has been created in the application folder"
+ "\n" + "Press ENTER key..");

Console.ReadLine();
}

}
}