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();
    // Retrieve nodes to match the expression
    XmlNodeList xnl = xd.DocumentElement.SelectNodes(txtXPath.Text);
    // And dump the results
    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);
    lbNodes.Items.Add(xnod.NodeType.ToString()+ ": " +
    xnod.Name + " = " + xnod.Value);

    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. Advertisements

  2. airwalker

    Cerebrus Guest


    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 :

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

    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)

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



    Cerebrus, Jun 28, 2006
    1. Advertisements

  3. airwalker

    airwalker Guest


    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
  4. Developping without making mistakes is failing to learn. Be proud, not
    Gorm Braarvig, Jun 28, 2006
  5. airwalker

    Cerebrus Guest

    in message

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.