Listing 1: Books.xml
Programming Microsoft .NET
Jeff Prosise
Applied Microsoft .NET Framework Programming
Jeffrey Richter
Windows Forms Programming in C#
Chris Sells
Essential .NET, Vol 1: The Common Language Runtime
Don Box
Listing 2: Using XPathDocument2 and XPathNavigator2 to read Books.xml
XPathDocument2 doc = new XPathDocument2 ();
doc.Load ("Books.xml");
XPathNavigator2 nav = doc.CreateXPathNavigator2 ();
IEnumerable iter = nav.Select ("/Books/Book/Name");
foreach (XPathNavigator2 node in iter)
{
Console.WriteLine (node.ReadValue ());
node.MoveToParent ();
if (node.HasAttributes)
{
node.MoveToFirstAttribute ();
Console.WriteLine ("\t {0}", node.ReadValue ());
}
}
Listing 3: Using XPathEditor and XmlTextWriter to modify an XML document
XPathDocument2 doc = new XPathDocument2 ();
doc.Load ("Books.xml");
XPathEditor editor = doc.CreateXPathEditor ();
editor.MoveToFirstChild ();
using (XmlWriter writer = editor.CreateFirstChild ())
{
writer.WriteStartElement ("Book");
writer.WriteAttributeString ("isbn", "0735620857");
writer.WriteElementString ("Name", "",
"Introducing Longhorn for Developers");
Writer.WriteElementString ("Author", "", "Brent
Rector");
writer.WriteEndElement ();
}
doc.Save ("Books.xml");
Listing 4: Books.xslt
| ISBN |
Name of book |
Author's name |
|
|
|
Listing 5: Applying Books.xslt to Books.xml using the XsltProcessor
XsltProcessor proc = new XsltProcessor ();
proc.Compile ("Books.xslt");
proc.Execute ("Books.xml", "Books.htm");
Listing 6: Authors.xml
Jeff Prosise
Jeffrey Richter
Chris Sells
Don Box
Listing 7: The new JustBooks.xml – captures authors by their id, not name
Programming Microsoft .NET
1
Applied Microsoft .NET Framework Programming
2
Windows Forms Programming in C#
3
Essential .NET, Vol 1: The Common Language Runtime
4
Listing 8: The XQuery that combines the JustBooks.xml and Authors.xml data (GetAllBooks.txt)
{
for $book in document('Books')//Book
return
{ $book/@isbn }
{ $book/Name/text() }
{
for $author in document('Authors')//Author
where ($book/AuthorId = $author/@id)
return { $author/Name/text() }
}
}
Listing 9: Applying the XQuery to Authors.xml and JustBooks.xml
System.Xml.XmlDataSourceResolver ds = new System.Xml.XmlDataSourceResolver ();
ds.Add ("Books", "JustBooks.xml");
ds.Add ("Authors", "Authors.xml");
XQueryProcessor xp = new XQueryProcessor ();
StreamReader r = new StreamReader ("GetAllBooks.txt");
StreamWriter w = new StreamWriter ("Result.xml");
xp.Compile (r);
xp.Execute (ds, w);