.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> XML >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Use of XPath - sample Code

Posted By:Pravin       Posted Date: March 16, 2012    Points: 40    Category: XML    URL: http://www.dotnetspark.com  

Here i have used to take value from tag in XML using XPath and binding it in dropdown control. For more your information ,i have posted a link regarding XPath, kindly review it for better understanding.
 

// Below is the xml code in "data.xml" file



 
    Empire Burlesque
    Bob Dylan
    10.90
 

 
    Hide your heart
    Bonnie Tyler
    10.0
 

 
    Greatest Hits
    Dolly Parton
    9.90
 




//Here is the function of XPath , Use this to call inside the Button Onclick event

protected void XpathFun()
    {
        string strfileName = "D:/Users/XPathProject/data.xml";
        XPathDocument doc = new XPathDocument(strfileName);
        XPathNavigator nav = doc.CreateNavigator();
        XPathExpression expr;
        expr = nav.Compile("catalog/cd/*");
        XPathNodeIterator iterator = nav.Select(expr);
        try
        {
            while (iterator.MoveNext())
            {
                XPathNavigator nav2 = iterator.Current.Clone();
                ddlList.Items.Add(nav2.Value);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }



// Output will be like this in dropdown control 'ddlList'
10.90
10.0
9.90

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend