Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Computer Certification > MCAD > XPath problem

Reply
Thread Tools

XPath problem

 
 
airwalker
Guest
Posts: n/a
 
      06-28-2006
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.

 
Reply With Quote
 
 
 
 
Cerebrus
Guest
Posts: n/a
 
      06-28-2006
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.

 
Reply With Quote
 
 
 
 
airwalker
Guest
Posts: n/a
 
      06-28-2006
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.
 
Reply With Quote
 
Gorm Braarvig
Guest
Posts: n/a
 
      06-28-2006

"airwalker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.

 
Reply With Quote
 
Cerebrus
Guest
Posts: n/a
 
      06-29-2006

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Memory leak" in javax.xml.xpath.XPath Marvin_123456 Java 4 07-29-2005 03:49 PM
XPath: efficiency in xpath expressions Tjerk Wolterink XML 1 11-13-2004 06:03 PM
Are there any XPath parsers that generate XPath trees? goog XML 0 01-14-2004 01:47 PM
XPath that does not include other XPath Anna XML 0 07-31-2003 07:55 AM
Problem selecting a node with XPATH if attribute value contains backslashes - how to force XPATH string to be treated as literal? Alastair Cameron XML 1 07-08-2003 07:24 PM



Advertisments