About query a node using XPath in C# program.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi Guys,
I need to query and select a node content using xpath.
This is a sample xml file.

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <?<span style="color:#A31515; xml <span style="color:Red; version<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1.0<span style="color:Black; " <span style="color:Red; encoding<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; ISO-8859-1<span style="color:Black; "<span style="color:Blue; ?>

<span style="color:Blue; <<span style="color:#A31515; bookstore<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; book <span style="color:Red; category<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; WEB<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; title <span style="color:Red; lang<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; en<span style="color:Black; "<span style="color:Blue; >XQuery Kick Start<span style="color:Blue; </<span style="color:#A31515; title<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; author<span style="color:Blue; >James McGovern<span style="color:Blue; </<span style="color:#A31515; author<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; year<span style="color:Blue; >2003<span style="color:Blue; </<span style="color:#A31515; year<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; price<span style="color:Blue; >49.99<span style="color:Blue; </<span style="color:#A31515; price<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; book<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; bookstore<span style="color:Blue; >
[/code]

<br/>
I need to get the title of book using C# in a windows form program. However, I cant get right result. Following is relational C# code.

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.Data;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; using System.IO;
<span style="color:Blue; using System.Xml;
<span style="color:Blue; using System.Xml.XPath;

<span style="color:Blue; namespace WindowsFormsApplication5
{
<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class Form1 : Form
{
<span style="color:Blue; public Form1()
{
InitializeComponent();
}

<span style="color:Blue; private <span style="color:Blue; void button1_Click(<span style="color:Blue; object sender, EventArgs e)
{
XPathNavigator nav;
XPathDocument docNav;
<span style="color:Blue; string strExpression;
docNav = <span style="color:Blue; new XPathDocument(<span style="color:#A31515; @"c:books.xml");
nav = docNav.CreateNavigator();
strExpression = <span style="color:#A31515; "/bookstore/book/title";
MessageBox.Show(nav.Evaluate(strExpression).ToString(), <span style="color:#A31515; "Title", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
[/code]
Where is my mistake? And how to correct?<br/>
<br/>


View the full article
 
Back
Top