Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XPath Queries in C#

Status
Not open for further replies.

rekenaar

Technical User
Feb 16, 2005
38
ZA
I am having a problem with my xpath queries in C#.
I have the following xml file:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<config>
  <data_module>
    <table_name>users</table_name>
    <fields>
      <field>
        <name>name</name>
        <type>string</type>
      </field>
    </fields>
    <select_sql>select * from users</select_sql>
  </data_module> 
</config>

When i try to select the select_sql like so:
Code:
// Doc is a XPathDocument already correctly loaded
 XPathNavigator nav = doc.CreateNavigator();
 XPathExpression expr = nav.Compile("/config/data_module/select_sql");
 XPathNodeIterator nodeIter = nav.Select(expr);
 String returnStr = (String)nodeIter.Current.Value;

The return String is "usernamestringselect * from users" where I want it to be "select * from users". I checked tutorials on XPath but it seems I must be doing it correctly but I have no idea what.

Thanks.
 
The way that I have always done it is like so:
Code:
[COLOR=blue]string[/color] returnString = 
doc.SelectSingleNode([COLOR=red]"config/data_module/select_sql"[/color]).InnerText;

Hope this helps!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top