Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > BOOK: Professional C#, 2nd and 3rd Editions
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 6th, 2006, 12:34 PM
Registered User
 
Join Date: Oct 2006
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?

 
Old October 6th, 2006, 12:37 PM
Registered User
 
Join Date: Oct 2006
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\"]"






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 08: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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.