Accessing data in XML through XPath and C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • machismo
    New Member
    • Sep 2006
    • 1

    #1

    Accessing data in XML through XPath and C#

    I have a following XML:
    Code:
    <?xml version="1.0" encoding="utf-8" ?> 
    <catalog>
    	<cd country="USA">
    		<title>ABCD</title>
    		<author>yash</author>
    		<price>10.90</price>
    	</cd>
    	<cd country="UK">
    		<title>XYZ</title>
    		<author>John</author>
    		<price>10.0</price>
    	</cd>
    	<cd country="USA">
    		<title>GreatHits</title>
    		<author>yash</author>
    		<price>9.90</price>
    	</cd>
    </catalog>
    I have filled a DropdownList with all the titles. Now how can I get the author and price of particular title when selected in the dropdownlist through XPath and C#.
    I am using following method:
    Code:
    string fileName="Catalog.xml";
    string title= DropDownList1.SelectedItem.ToString();
    XmlDocument xdoc=new XmlDocument();
    xdoc.Load(@"C:\Inetpub\Wwwroot\Xpath1\" +fileName);
    string price= xdoc.SelectSingleNode("/catalog/cd/price[title='" + title "']").Value;
    but this is not helping. It returns Full InnerXml of the file.
    Last edited by Dormilich; Mar 7 '09, 07:10 PM. Reason: added [code] tags
Working...