Tuesday, August 9, 2011

Query Xml File with Namespace using XPath



We all know how to get value from xml file in .net but we face issue when there is a namespace with that xml file.

When a xml file contains a namespace then you will have to do some extra efforts to retrieve its value.

Lets say my xml is like:



Videos
119150

207463
somepath
sometitle
somelink
...

http://someurl/standard-and-poors-india-connection/207463

Tue, 09 Aug 2011 6:26:48 IST
1312894608




Now i wants to access value of "url" attribute of "media:thumbnail" tag, so to get it you will have to do :

Dim path As String = "http://api.flickr.com/services/feeds/photoset.gne?set=72157604607980332&nsid=51374889@N00&lang=en-us&format=rss_200_enc"
        Dim xmldoc As New XmlDocument()
        xmldoc.Load(path)
        Dim xnm As New XmlNamespaceManager(xmldoc.NameTable)
        xnm.AddNamespace("media", "http://search.yahoo.com/mrss/")
        Dim nList As XmlNodeList = xmldoc.SelectNodes("//item/media:thumbnail/@url", xnm)

        For Each xNode As XmlNode In nList
            ListBox1.Items.Add(xNode.InnerText)
            TextBox1.Text += TextBox1.Text + xNode.InnerText + vbNewLine
        Next


Now comes to original problem for which i have done google for 2-3 hours and then finally i got the answer.

Problem: I have got a repeater and i wants to show this xml values in it. now 1 option is that you can convert it to a datatable and easily use it.. but it will be extra process to you application. so now we wants to show xml directly to the repeater... to do so see below code:

In code behind file:
XmlDataSource xds = new XmlDataSource();
xds.XPath = "rss/channel/item";
xds.DataFile = sourceXml;// you can write xml path here.
myrpt.DataSource = xds;
myrpt.DataBind();

Now in .aspx file write below code:
< asp:Repeater ID="myrpt" runat="server" >
                < ItemTemplate >
                    < li >
                        < div class="multi_div" >
                            < img src='< %#XPath("*[local-name()='thumbnail' and namespace-uri()='http://search.yahoo.com/mrss/']/@url")% >' class="imgbdr" / >< div class="multi_blkstrip" >
                                < p class="float_l" >
                                    < img src="images/video_icon.gif" / >< /p >
                                < p class="float_r" >
                                    < %#XPath("title")% >< /p >
                            < /div >
                        < /div >
                        < p class="vidcap" >
                            < a href="#" class="fn fl" >< %#XPath("title")% >< /a >< /p >
                    < /li >
                < /ItemTemplate >
            < /asp:Repeater >

so this is the main line (solution):

< %#XPath("*[local-name()='thumbnail' and namespace-uri()='http://search.yahoo.com/mrss/']/@url")% >'

Compiled By: Rajesh Rolen

Share This!


1 comment:

Unknown said...

Well done! I scoured the forums for a good two hours before realizing this very elegant and simplifed approach. "*[local-name()="... brilliant.

Powered By Blogger · Designed By Seo Blogger Templates