Mega Code Archive

 
Categories / C# / XML
 

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> */