没有从XML文件返回数据 [英] No data is returned from XML file

查看:87
本文介绍了没有从XML文件返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从API xml文件(URL)读取数据。链接工作并显示原始数据,但在下面的页面中使用Repeater控件时,没有显示任何属性(ns2:description)我试图访问它一个例子,所以我应该能够访问文件中的任何属性。任何人都可以建议如何更正这个?



I am trying to read data from an API xml file (URL). The link works and shows the raw data but When using the Repeater control in the page below, nothing is shown The attribute (ns2:description) I am trying to access it an example so I should be able to access any attribute in the file. Can anyone advise on how to correct this ?

<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Xml" %>
<%@ import Namespace="System.Net.HttpWebResponse" %>


<Script runat="server">



sub Page_Load
if Not Page.IsPostBack then


 Dim doc  as XmlDocument = new XmlDocument()
doc.Load("http://api.tradedoubler.com/1.0/products.xml?token=96CC0E0A10851500F10431D64EC5585BFC8597DF")
Dim nodes As XmlNodeList = doc.SelectNodes("products/product")
rpMyRepeater.DataSource = nodes
rpMyRepeater.DataBind()

end if
end sub


</script>


<html>
<body>
<asp:repeater id="rpMyRepeater" runat="server">
<HeaderTemplate>
   <Table border="0">
</HeaderTemplate>
   <ItemTemplate>
   <tr style="background-color:FFECD8">
      <td>




<![CDATA[<%# DirectCast(Container.DataItem, System.Xml.XmlNode).Attributes("ns2:description").Value %>]]>


      </td>
   </tr>
   </ItemTemplate>
   <FooterTemplate>
      </Table>
   </FooterTemplate>
</asp:repeater>

推荐答案

正如George所说,这是命名空间问题。不幸的是, XmlDocument 类不容易使用XML命名空间,特别是对于具有默认命名空间的文档。您必须创建 XmlNamespaceManager ,显式添加名称空间,并为默认名称空间赋予前缀。



另外,查看源文档,您尝试检索的 ns2:description 是一个元素,因此您将无法在<$ c中找到它$ c>属性集合。



因为你需要 XmlNamespaceManager 来检索子元素,最简单的选项可能是将节点投影到 Page_Load 方法中的自定义类型。



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>





注意:要使用LINQ查询,您可能需要导入 System.Linq 命名空间,如果它尚未导入 web.conf ig file:



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" %>


这篇关于没有从XML文件返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆