As George said, this is a namespace issue. Unfortunately, the
XmlDocument
class doesn't make it easy to work with XML namespaces, and particularly with documents which have a default namespace. You have to create an
XmlNamespaceManager
, explicitly add the namespaces, and give the default namespace a prefix.
Also, looking at the source document, the
ns2:description
that you're trying to retrieve is an element, so you won't be able to find it in the
Attributes
collection.
Since you need the
XmlNamespaceManager
to retrieve the child elements, the simplest option is probably to project the nodes to a custom type in the
Page_Load
method.
Dim doc As New XmlDocument()
doc.Load("http://api.tradedoubler.com/1.0/products.xml?token=96CC0E0A10851500F10431D64EC5585BFC8597DF")
Dim nsManager As New XmlNamespaceManager(doc.NameTable)
nsManager.AddNamespace("ns1", "urn:com:tradedoubler:pf:model:xml:output")
nsManager.AddNamespace("ns2", "urn:com:tradedoubler:pf:model:xml:common")
Dim nodes As XmlNodeList = doc.SelectNodes("//ns1:products/ns1:product", nsManager)
Dim dataSource As IEnumerable
dataSource = From node As XmlNode in nodes
Select Name = node.SelectSingleNode("ns2:name", nsManager).InnerText, _
Description = node.SelectSingleNode("ns2:description", nsManager).InnerText, _
Image = node.SelectSingleNode("ns2:productImage", nsManager).InnerText
rpMyRepeater.DataSource = dataSource
rpMyRepeater.DataBind()
<asp:repeater id="rpMyRepeater" runat="server">
<HeaderTemplate>
<table border="0">
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color:FFECD8">
<td>
<%# Eval("Name") %>
</td>
<td>
<%# Eval("Description") %>
</td>
<td>
<asp:image runat="server"
ImageUrl='<%# Eval("Image") %>'
/>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:repeater>
NB: To use the LINQ query, you might need to import the
System.Linq
namespace, if it's not already imported in your
web.config
file:
<%@ Import Namespace="System.Linq" %>