Hi,
I've run several XPath queries against an XML document in a program such as XML Spy with great success. When I run the same queries in a small app I wrote that uses the System.Xml.XPath I get mixed results.
I'd like to know what I have to do to get my XPath queries to execute properly.
I want to use this class to extract parts of an XML document sent over the web to create various class instances. If there's a better way of doing this, feel free to suggest it!
Here's the XML doc.
Code:
<PersonProfile>
<Table>
<id>1</id>
<firstname>Michael</firstname>
<lastname>Gradek</lastname>
<birthdate>1980-11-13T00:00:00.0000000-05:00</birthdate>
</Table>
</PersonProfile>
Here's the class that executes the XPath query
Code:
using System;
using System.Xml;
using System.Xml.XPath;
namespace blackbook
{
/// <summary>
/// Allows for quick queries on an XML document using XPath.
/// </summary>
class XmlQuerier
{
private XPathDocument xpathdoc;
private XPathNavigator xpathnav;
/// <summary>
/// Creates an XmlQuerier
/// </summary>
/// <param name="xmlurl">the url from which to get the XML document</param>
public XmlQuerier(string xmlurl)
{
xpathdoc = new XPathDocument(xmlurl);
xpathnav = xpathdoc.CreateNavigator();
}
/// <summary>
/// Creates an XmlQuerier
/// </summary>
/// <param name="xmltext">The <seealso cref="System.Xml.XmlTextReader"/>XmlTextReader
/// from which to get the XML document</param>
public XmlQuerier(XmlTextReader xmltext)
{
xpathdoc = new XPathDocument(xmltext);
xpathnav = xpathdoc.CreateNavigator();
}
/// <summary>
/// Gets a variable length array of strings corresponding to the XPath query results
/// </summary>
/// <param name="selectexpr">The XPath expression used to query. The XPath must be properly formed.</param>
/// <returns>A variable length array of strings</returns>
public string[] XPathQuery(string selectexpr)
{
string[] response;
XPathExpression xpathexpr;
XPathNodeIterator xpathnode;
try
{
// Create a node interator to select nodes and move through them (read-only)
this.xpathnav.MoveToRoot();
xpathexpr = this.xpathnav.Compile(selectexpr);
xpathnode = xpathnav.Select (xpathexpr);
response = new string[xpathnode.Count];
for(int i = 0; i < xpathnode.Count; i++, xpathnode.MoveNext())
response[i] = xpathnode.Current.Value; }
catch (System.Xml.XmlException e)
{
response = new string[1];
response[0] = e.ToString();
}
return response;
}
}
}
Here's the app code that calls the xml query
Code:
using System;
using System.Xml;
using System.Xml.XPath;
namespace xmlquery
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class QueryTest
{
public static string url = @"C:\Documents and Settings\Mike Gradek\Desktop\Test\person.xml";
[STAThread]
static void Main(string[] args)
{
if(args.Length != 1)
Console.WriteLine("Usage: xmlquery.exe <xpath query>");
else
{
QueryTest querytest = new QueryTest();
querytest.Run(QueryTest.url, args[0]);
}
}
public void Run(string url, string query)
{
Console.WriteLine("XPath Test started ...");
blackbook.XmlQuerier xmlquery = new blackbook.XmlQuerier(url);
Console.WriteLine("Query: " + query);
foreach(string s in xmlquery.XPathQuery(query))
Console.WriteLine(s);
}
}
}
Here's the XPath query I run in XML Spy
//firstname
which yields the firstname.
When I run the same query in my little app, I get the entire document content returned as a string. Here's the program output
prompt> xmlquery.exe //firstname
XPath Test started ...
Query: //firstname
1MichaelGradek1980-11-13T00:00:00.0000000-05:00
But it should return
XPath Test started ...
Query: //firstname
Michael Gradek
What is it that I'm doing wrong? I'm simply trying to extract the contents of nodes through XPath queries...
Any insight would be greatly appreciated!
Thanks,
Mike