{"id":581,"date":"2007-07-06T16:45:05","date_gmt":"2007-07-06T15:45:05","guid":{"rendered":"http:\/\/www.craigmurphy.com\/blog\/?p=581"},"modified":"2007-07-06T16:59:24","modified_gmt":"2007-07-06T15:59:24","slug":"net-xml-and-xpath","status":"publish","type":"post","link":"http:\/\/www.craigmurphy.com\/blog\/?p=581","title":{"rendered":".NET &#8211; XML and XPath"},"content":{"rendered":"<p>I have been receiving a few requests for my now elderly <a href=\"http:\/\/www.craigmurphy.com\/bug\">Delphi XML\/XPath<\/a> examples to be brought into the world of .NET.  Similarly, I have seen a lot of newsgroup posts about using XML and XPath expressions, particularly those XPath expressions that can be used to &#8220;query&#8221; the XML &#8220;database&#8221;.<\/p>\n<p>The most popular request seems to have been for my &#8220;employee&#8221; selector demonstration:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.craigmurphy.com\/blog\/wp-content\/uploads\/2007\/07\/XMLTestBed.gif\" \/><\/p>\n<p>It&#8217;s a small application that lets you load some XML (employee data) into an XML document.  It then lets you fire a handful of XPath expressions at the XML document via the use of the SelectNode methods.  It demonstrates selecting specific employees using a combination of conditions; there&#8217;s a mixture of XPath that looks at element values and one that looks at an attribute value.  I&#8217;ve revamped it slightly, noteably I&#8217;ve made a few minor changes to bring it into line with current W3C standard (as enforced by .NET&#8217;s <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/system.xml.xmlnode.selectnodes(vs.71).aspx\">SelectNodes<\/a> method).<\/p>\n<p>[code lang=&#8221;XML&#8221;]<br \/>\n[XML]<br \/>\n<?xml version=\"1.0\"?><br \/>\n<employees><br \/>\n<employee emp_no=\"2\"><br \/>\n<emp_lastname>Nelson<\/emp_lastname><br \/>\n<emp_firstname>Roberto<\/emp_firstname><br \/>\n<emp_phoneext>250<\/emp_phoneext><br \/>\n<emp_salary currency=\"UKP\">40000<\/emp_salary><br \/>\n<\/employee><\/p>\n<p><employee emp_no=\"4\"><br \/>\n<emp_lastname>Young<\/emp_lastname><br \/>\n<emp_firstname>Bruce<\/emp_firstname><br \/>\n<emp_phoneext>233<\/emp_phoneext><br \/>\n<emp_salary currency=\"USD\">55500<\/emp_salary><br \/>\n<\/employee><br \/>\n&#8230;<br \/>\n<\/employees><br \/>\n[\/code]<\/p>\n<p>[code lang=&#8221;C#&#8221;]<br \/>\n[C#]<br \/>\nusing System;<br \/>\nusing System.Collections.Generic;<br \/>\nusing System.ComponentModel;<br \/>\nusing System.Data;<br \/>\nusing System.Drawing;<br \/>\nusing System.Text;<br \/>\nusing System.Windows.Forms;<\/p>\n<p>using System.Xml;<\/p>\n<p>namespace XML<br \/>\n{<br \/>\n    public partial class Form1 : Form<br \/>\n    {<br \/>\n        XmlDocument doc;<\/p>\n<p>        public Form1()<br \/>\n        {<br \/>\n            InitializeComponent();<br \/>\n        }<\/p>\n<p>        private void button1_Click(object sender, EventArgs e)<br \/>\n        {<br \/>\n            doc = new XmlDocument();<br \/>\n            doc.Load(&#8220;employees.xml&#8221;);<\/p>\n<p>            textBox1.Text = doc.OuterXml;<br \/>\n        }<\/p>\n<p>        private void handle_xpath(String xPathExpression)<br \/>\n        {<br \/>\n            XmlNodeList result;<br \/>\n            XmlNode root = doc.DocumentElement;<\/p>\n<p>            result = root.SelectNodes(xPathExpression);<\/p>\n<p>            label1.Text = String.Format(&#8220;{0} items returned&#8221;, result.Count);<\/p>\n<p>            textBox3.Clear();<br \/>\n            foreach (XmlNode x in result)<br \/>\n            {<br \/>\n                textBox3.Text = textBox3.Text + x.OuterXml + Environment.NewLine + Environment.NewLine;<br \/>\n            }<br \/>\n        }<\/p>\n<p>        private void button2_Click(object sender, EventArgs e)<br \/>\n        {<br \/>\n            handle_xpath(&#8220;.\/\/employee&#8221;);<br \/>\n        }<\/p>\n<p>        private void button3_Click(object sender, EventArgs e)<br \/>\n        {<br \/>\n            handle_xpath(&#8220;\/employees&#8221;);<br \/>\n        }<\/p>\n<p>        private void button4_Click(object sender, EventArgs e)<br \/>\n        {<br \/>\n            handle_xpath(&#8220;\/employees\/employee[1]&#8221;);<br \/>\n        }<\/p>\n<p>        private void button5_Click(object sender, EventArgs e)<br \/>\n        {<br \/>\n            handle_xpath(&#8220;\/employees\/employee[last()]&#8221;);<br \/>\n        }<\/p>\n<p>        private void button6_Click(object sender, EventArgs e)<br \/>\n        {<br \/>\n            handle_xpath(&#8220;\/employees\/employee[emp_salary>30000 and emp_salary<35000]\");\n        }\n\n        private void button7_Click(object sender, EventArgs e)\n        {\n            handle_xpath(\"\/employees\/employee[emp_salary>30000 and emp_salary<35000 and emp_salary[@currency='UKP']]\");\n        }\n\n        private void button8_Click(object sender, EventArgs e)\n        {\n            handle_xpath(\"\/employees\/employee[emp_salary > 50000]\/emp_lastname&#8221;);<br \/>\n        }<\/p>\n<p>        private void button9_Click(object sender, EventArgs e)<br \/>\n        {<br \/>\n            handle_xpath(textBox2.Text);<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>The source code is available <a href=\"http:\/\/www.craigmurphy.com\/blog\/wp-content\/uploads\/2007\/07\/XML.zip\">here<\/a> [60k].<\/p>\n<p>There&#8217;s a short 60 second movie of the application in use <a href=\"http:\/\/www.craigmurphy.com\/blog\/wp-content\/uploads\/2007\/07\/XMLAndXPath.wmv\">here<\/a> [748kb]. Courtesy of TechSmith&#8217;s <a href=\"http:\/\/www.techsmith.com\">Camtasia<\/a>.<\/p>\n<p>Technorati Tags: <a href=\"http:\/\/technorati.com\/tag\/XML\" rel=\"tag\">XML<\/a>, <a href=\"http:\/\/technorati.com\/tag\/XPath\" rel=\"tag\">XPath<\/a>, <a href=\"http:\/\/technorati.com\/tag\/C%23\" rel=\"tag\">C#<\/a>, <a href=\"http:\/\/technorati.com\/tag\/C-Sharp\" rel=\"tag\">C-Sharp<\/a>, <a href=\"http:\/\/technorati.com\/tag\/SelectNodes\" rel=\"tag\">SelectNodes<\/a>, <a href=\"http:\/\/technorati.com\/tag\/VS2005\" rel=\"tag\">VS2005<\/a>, <a href=\"http:\/\/technorati.com\/tag\/Visual+Studio+2005\" rel=\"tag\">Visual Studio 2005<\/a>, <a href=\"http:\/\/technorati.com\/tag\/Visual+C%23+Express\" rel=\"tag\">Visual C# Express<\/a>, <a href=\"http:\/\/technorati.com\/tag\/Camtasia\" rel=\"tag\">Camtasia<\/a>, <a href=\"http:\/\/technorati.com\/tag\/TechSmith\" rel=\"tag\">TechSmith<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have been receiving a few requests for my now elderly Delphi XML\/XPath examples to be brought into the world of .NET. Similarly, I have seen a lot of newsgroup posts about using XML and XPath expressions, particularly those XPath expressions that can be used to &#8220;query&#8221; the XML &#8220;database&#8221;. The most popular request seems &hellip; <a href=\"http:\/\/www.craigmurphy.com\/blog\/?p=581\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">.NET &#8211; XML and XPath<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,3],"tags":[],"class_list":["post-581","post","type-post","status-publish","format-standard","hentry","category-net","category-development"],"_links":{"self":[{"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/581","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=581"}],"version-history":[{"count":0,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/581\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=581"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.craigmurphy.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}