Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 15th, 2003, 10:53 AM
Registered User
 
Join Date: Jun 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default .NET XPath query not coming out right

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
xpath query blitzer XSLT 2 May 19th, 2007 03:54 AM
Problem coming while deploying asp.net application deeptisingh20 ASP.NET 1.0 and 1.1 Professional 1 April 10th, 2006 10:06 AM
Problem coming while installing asp.net project deeptisingh20 ASP.NET 1.0 and 1.1 Professional 2 April 10th, 2006 06:25 AM
Read xpath query from node value jaquing XSLT 2 January 11th, 2006 06:50 PM
Binding Results of an Xpath query to a datagrid cowa ADO.NET 2 November 17th, 2003 02:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.