XPath problem

Discussion in 'MCAD' started by airwalker, Jun 28, 2006.

  1. airwalker

    airwalker Guest

    Im revising to take the 70-320 exam,
    I got a problem with some xml code. In quite certain the error is easily
    solved, but I just dont know how as im new to xml.

    The code im using is to enter an XPath statement into a text box on a
    windows form.

    private void btnEvaluate_Click(object sender, System.EventArgs e)
    {
    // Load the Books.xml file
    XmlTextReader xtr = new XmlTextReader(@"..\..\Books.xml");
    xtr.WhitespaceHandling = WhitespaceHandling.None;
    XmlDocument xd = new XmlDocument();
    xd.Load(xtr);
    // Retrieve nodes to match the expression
    XmlNodeList xnl = xd.DocumentElement.SelectNodes(txtXPath.Text);
    // And dump the results
    lbNodes.Items.Clear();
    foreach (XmlNode xnod in xnl)
    // For elements, display the corresponding text entity
    if (xnod.NodeType == XmlNodeType.Element)
    lbNodes.Items.Add(xnod.NodeType.ToString() + ": " +
    xnod.Name + " = " + xnod.FirstChild.Value);
    else
    lbNodes.Items.Add(xnod.NodeType.ToString()+ ": " +
    xnod.Name + " = " + xnod.Value);
    xtr.Close();
    }

    when I execute the program and enter an XPath statement in the text box I
    get this error message:

    "An unhandled exception of type 'System.Xml.XmlException' occurred in
    system.xml.dll Additional information: System error."

    also when I put the mouse curser over the line:

    XmlNodeList xnl = xd.DocumentElement.SelectNodes(txtXPath.Text);

    I get this message:

    "xnl =<undefined value>

    any suggestions would be greatly appreciated.
     
    airwalker, Jun 28, 2006
    #1
    1. Advertising

  2. airwalker

    Cerebrus Guest

    Hi,

    The code is perfectly fine. This is Step by Step 2.7 in the Amit Kalani
    book, I suppose ? ;-)

    The problem as far as I can tell, lies in the XPath expression you are
    entering. It is an invalid expression, therefore, you are getting this
    error. Also, remember that XPath is case sensitive. The names must
    exactly match the names of elements in the XML file.

    Try putting the whole segment in a Try-Catch construct, as follows :

    Try
    {
    ....
    }
    Catch System.Xml.XPath.XPathException ex
    {
    MessageBox.Show("Incorrect XPath expression. Please try again...
    Details :" & ex.Message, "Error !", MessageBoxButtons.OK,
    MessageBoxIcon.Error);
    }

    If it doesn't work, please post the XPath expression you are trying to
    evaluate. Here are some that worked for me, when I did this exercise.
    (long back)

    1. /BOOKS/BOOK/NAME
    2. //NAME
    3. /BOOKS/BOOK/@Pages

    HTH,

    Regards,

    Cerebrus.
     
    Cerebrus, Jun 28, 2006
    #2
    1. Advertising

  3. airwalker

    airwalker Guest

    Cerebrus,

    thanks for ur reply. The problem lied in my xml file, I had one too many
    closing tags right down at the bottom, so far down I didnt see it.

    a silly mistake to make, im ashamed of myself lol.
     
    airwalker, Jun 28, 2006
    #3
  4. "airwalker" <> wrote in message
    news:...
    > Cerebrus,
    >
    > thanks for ur reply. The problem lied in my xml file, I had one too many
    > closing tags right down at the bottom, so far down I didnt see it.
    >
    > a silly mistake to make, im ashamed of myself lol.


    Developping without making mistakes is failing to learn. Be proud, not
    ashamed.
     
    Gorm Braarvig, Jun 28, 2006
    #4
  5. airwalker

    Cerebrus Guest

    "airwalker" wrote in message

    > > Cerebrus,
    > >
    > > thanks for ur reply. The problem lied in my xml file, I had one too many
    > > closing tags right down at the bottom, so far down I didnt see it.
    > >
    > > a silly mistake to make, im ashamed of myself lol.


    Ah, no prob, buddy. Feel free to ask anytime. ;-)
     
    Cerebrus, Jun 29, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom Accuosti
    Replies:
    3
    Views:
    502
    Travis Evans
    Sep 27, 2004
  2. Vic

    Setup MSN Shorcut problem and IE problem

    Vic, May 11, 2004, in forum: Computer Support
    Replies:
    1
    Views:
    732
    ┬░Mike┬░
    May 11, 2004
  3. Replies:
    4
    Views:
    12,461
  4. juanita_guiterrez

    xslt/xpath string manipulation

    juanita_guiterrez, Jul 29, 2006, in forum: Software
    Replies:
    0
    Views:
    1,582
    juanita_guiterrez
    Jul 29, 2006
  5. sandhya

    XPath expression in PERL

    sandhya, Mar 27, 2010, in forum: Software
    Replies:
    0
    Views:
    1,322
    sandhya
    Mar 27, 2010
Loading...

Share This Page