using System;
using System.Xml;
public class SellItems {
static void Main(string [] args) {
XmlDocument inventory = new XmlDocument();
inventory.Load("inventory.xml");
XmlNodeList elements = inventory.SelectNodes("//inventory/items/item");
foreach (XmlElement element in elements) {
string productCode = element.GetAttribute("productCode");
int quantitySold = Int32.Parse(element.GetAttribute("quantity"));
Console.WriteLine(quantitySold);
string xPathExpression = "//items/item[@productCode='" + productCode + "']";
XmlElement inventoryItem = (XmlElement)inventory.SelectSingleNode(xPathExpression);
int quantity = Int32.Parse(inventoryItem.GetAttribute("quantity"));
quantity -= quantitySold;
inventoryItem.SetAttribute("quantity",quantity.ToString());
}
inventory.Save("inventory.xml");
}
}
/*
<inventory>
<date year="2006" month="8" day="27" />
<items>
<item quantity="5" productCode="01" description="PHP" unitCost="9.95" />
<item quantity="3" productCode="02" description="Perl" unitCost="4.95" />
</items>
</inventory>
*/
Month: February 2011
XPath Query Demo
using System;
using System.Xml;
using System.Xml.XPath;
public class XPathQuery {
public static void Main(string [] args) {
string filename = "inventory.xml";
string xpathExpression = "//inventory/items";
XmlDocument document = new XmlDocument( );
document.Load(filename);
XmlTextWriter writer = new XmlTextWriter(Console.Out);
writer.Formatting = Formatting.Indented;
XmlNode node = document.SelectSingleNode(xpathExpression);
node.WriteTo(writer);
writer.Close( );
}
}
/*
<inventory>
<date year="2006" month="8" day="27" />
<items>
<item quantity="5" productCode="01" description="PHP" unitCost="9.95" />
<item quantity="3" productCode="02" description="Perl" unitCost="4.95" />
</items>
</inventory>
*/
Find Elements with an XPath Search
using System;
using System.Xml;
public class XPathSelectNodes {
private static void Main() {
// Load the document.
XmlDocument doc = new XmlDocument();
doc.Load("books.xml");
XmlNodeList nodes = doc.SelectNodes("/books/A/B");
foreach (XmlNode node in nodes) {
Console.WriteLine(node.InnerText);
}
}
}
/*
<books>
<A property="a">
<B>text</B>
<C>textg</C>
<D>99999</D>
</A>
</books>
*/
Select by path
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
public class MainClass {
public static void Main() {
XmlDocument doc = new XmlDocument();
doc.Load("books.xml");
XPathNavigator nav = doc.CreateNavigator();
if (nav.CanEdit) {
XPathNodeIterator iter = nav.Select("/bookstore/book/price");
while (iter.MoveNext()) {
iter.Current.InsertAfter("<disc>5</disc>");
}
}
doc.Save("newbooks.xml");
}
}
XPathNodeIterator
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
public class MainClass {
public static void Main() {
XPathDocument doc = new XPathDocument("books.xml");
XPathNavigator nav = ((IXPathNavigable)doc).CreateNavigator();
XPathNodeIterator iter = nav.Select("/bookstore/book");
while (iter.MoveNext()) {
XPathNodeIterator newIter = iter.Current.SelectDescendants(XPathNodeType.Element, false);
while (newIter.MoveNext())
Console.WriteLine(newIter.Current.Name + ": " + newIter.Current.Value);
}
Console.WriteLine("Total Cost = " + nav.Evaluate("sum(/bookstore/book/price)"));
}
}
XPathNavigator
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
public class MainClass {
public static void Main() {
XPathDocument doc = new XPathDocument("books.xml");
XPathNavigator nav = ((IXPathNavigable)doc).CreateNavigator();
XPathNodeIterator iter = nav.Select("/bookstore/book[@genre='novel']");
while (iter.MoveNext()) {
XPathNodeIterator newIter = iter.Current.SelectDescendants(XPathNodeType.Element, false);
while (newIter.MoveNext())
Console.WriteLine(newIter.Current.Name + ": " + newIter.Current.Value);
}
}
}
Add XmlElement
//Microsoft Public License (Ms-PL)
//http://dbmlmanager.codeplex.com/license
#region using
using System;
using System.Xml;
#endregion
namespace DbmlManager.Lib.Utility
{
#region Class Docs
/// <summary>
/// Summary description for XmlUtil.
/// </summary>
#endregion
public class XmlUtil
{
// Public Static Methods
#region AddXmlElement(XmlDocument xml, XmlNode parent, string nodeName, string nodeText)
public static void AddXmlElement(XmlDocument xml, XmlNode parent, string nodeName, string nodeText)
{
XmlElement node = xml.CreateElement(nodeName);
node.InnerText = nodeText;
parent.AppendChild(node);
}
#endregion
}
}

