Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > BOOK: Professional C#, 2nd and 3rd Editions
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional C#, 2nd and 3rd Editions
This is the forum to discuss the Wrox book Professional C#, 3rd Edition by Simon Robinson, Christian Nagel, Karli Watson, Jay Glynn, Morgan Skinner, Bill Evjen; ISBN: 9780764557590
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional C#, 2nd and 3rd Editions section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old October 6th, 2006, 01:34 PM
Registered User
 
Join Date: Oct 2006
Location: Beijing, Hebei, China.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to guozai
Default XML: How to select a node with ' in it using Xpath

This is a sample code named ADOSample1 which is not working correctly. (in 3rd Edition)

Need to find a node using ProductName in the following xml file

<?xml version="1.0" standalone="yes"?>
<XMLProducts>
  <products>
    <ProductID>1</ProductID>
    <ProductName>Chai</ProductName>
    <SupplierID>1</SupplierID>
    <CategoryID>1</CategoryID>
    <QuantityPerUnit>10 boxes x 20 bags</QuantityPerUnit>
    <UnitPrice>18</UnitPrice>
    <UnitsInStock>39</UnitsInStock>
    <UnitsOnOrder>0</UnitsOnOrder>
    <ReorderLevel>10</ReorderLevel>
    <Discontinued>false</Discontinued>
  </products>
  <products>
    <ProductID>2</ProductID>
    <ProductName>Chang</ProductName>
    <SupplierID>1</SupplierID>
    <CategoryID>1</CategoryID>
    <QuantityPerUnit>24 - 12 oz bottles</QuantityPerUnit>
    <UnitPrice>19</UnitPrice>
    <UnitsInStock>17</UnitsInStock>
    <UnitsOnOrder>40</UnitsOnOrder>
    <ReorderLevel>25</ReorderLevel>
    <Discontinued>false</Discontinued>
  </products>
  <products>
    <ProductID>3</ProductID>
    <ProductName>Aniseed Syrup</ProductName>
    <SupplierID>1</SupplierID>
    <CategoryID>2</CategoryID>
    <QuantityPerUnit>12 - 550 ml bottles</QuantityPerUnit>
    <UnitPrice>10</UnitPrice>
    <UnitsInStock>13</UnitsInStock>
    <UnitsOnOrder>70</UnitsOnOrder>
    <ReorderLevel>25</ReorderLevel>
    <Discontinued>false</Discontinued>
  </products>
  <products>
    <ProductID>4</ProductID>
    <ProductName>Chef Anton's Cajun Seasoning</ProductName>
    <SupplierID>2</SupplierID>
    <CategoryID>2</CategoryID>
    <QuantityPerUnit>48 - 6 oz jars</QuantityPerUnit>
    <UnitPrice>22</UnitPrice>
    <UnitsInStock>53</UnitsInStock>
    <UnitsOnOrder>0</UnitsOnOrder>
    <ReorderLevel>0</ReorderLevel>
    <Discontinued>false</Discontinued>
  </products>
  <products>
    <ProductID>5</ProductID>
    <ProductName>Chef Anton's Gumbo Mix</ProductName>
    <SupplierID>2</SupplierID>
    <CategoryID>2</CategoryID>
    <QuantityPerUnit>36 boxes</QuantityPerUnit>
    <UnitPrice>21.35</UnitPrice>
    <UnitsInStock>0</UnitsInStock>
    <UnitsOnOrder>0</UnitsOnOrder>
    <ReorderLevel>0</ReorderLevel>
    <Discontinued>true</Discontinued>
  </products>
</XMLProducts>

The code is the following:
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            //when you click on the listbox,
            //a message box appears with the unit price
            string srch = "XMLProducts/products[ProductName='"+
                listBox1.SelectedItem.ToString()+"']";
            //string srch="XMLProducts/Products[ProductName=" + '"' + listBox1.SelectedItem.ToString() + '"' + "]";
            XmlNode foundNode = doc.SelectSingleNode(srch);
            if(foundNode!=null)
                MessageBox.Show(foundNode.SelectSingleNode("UnitPr ice").InnerText);
            else
                MessageBox.Show("Not found");
        }

Using "XMLProducts/products[ProductName='"+ listBox1.SelectedItem.ToString()+"']";
works well for <ProductName>Aniseed Syrup</ProductName>

However, for ProductName which has ' mark within (eg, <ProductName>Chef Anton's Cajun Seasoning</ProductName>), this expression doesn't work. The sample code originally writes "XMLProducts/Products[ProductName=" + '"' + listBox1.SelectedItem.ToString() + '"' + "]";
which does not work at all!!!

What's the right expression for this situation?

Reply With Quote
  #2 (permalink)  
Old October 6th, 2006, 01:37 PM
Registered User
 
Join Date: Oct 2006
Location: Beijing, Hebei, China.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to guozai
Default

Addtional notes:
Using the sample Xpath "XMLProducts/Products[ProductName=" + '"' + listBox1.SelectedItem.ToString() + '"' + "]"
the result for <ProductName>Chef Anton's Cajun Seasoning</ProductName> is "XMLProducts/Products[ProductName=\"Chef Anton's Cajun Seasoning\"]"

Reply With Quote
Reply


Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Select XML Node - Name space qualified aintvoguish XSLT 2 December 28th, 2007 03:45 PM
XPath to get next node (sibling) jeft0nes XSLT 2 August 8th, 2007 09:30 PM
XPath node selection nkuar XML 1 March 4th, 2006 09:40 PM
Read xpath query from node value jaquing XSLT 2 January 11th, 2006 06:50 PM
Access Namespace Node using XPath pvasudevan XSLT 1 January 2nd, 2006 06:20 AM



All times are GMT -4. The time now is 03:43 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.