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?
|