1

我试图在我的 asp.NET 页面上显示以下 XML 文件,但它不起作用。

<Calendar>
  <Info>
    <CalendarID>95</CalendarID>
    <CalendarName>
      <![CDATA[ Kalender Jebjerg ]]>
    </CalendarName>
    <AppointmentInterval>1 days</AppointmentInterval>
    <RefreshInterval>5 minutes</RefreshInterval>
    <FreeAccessUntil>2013-01-15 09:12:27</FreeAccessUntil>
    <NextExportAvailable>2013-09-11 17:58:56</NextExportAvailable>
  </Info>
  <Appointment>
    <Ressource>
      <![CDATA[ Hallen ]]>
    </Ressource>
    <StartDate>2013-09-11 15:00:00</StartDate>
    <EndDate>2013-09-11 18:00:00</EndDate>
    <Subject>
      <![CDATA[ KIG - Gymnastik ]]>
    </Subject>
    <Description>
      <![CDATA[ ]]>
    </Description>
  </Appointment>
  <Appointment>
    <Ressource>
     <![CDATA[ KIG ]]>
    </Ressource>
    <StartDate>2013-09-11 15:00:00</StartDate>
    <EndDate>2013-09-11 19:00:00</EndDate>
    <Subject>
      <![CDATA[ KIG - Gymnastik ]]>
    </Subject>
    <Description>
      <![CDATA[ ]]>
    </Description>
  </Appointment>
</Calendar>

我已经用虚拟 XML 文件测试了我的 .aspx 页面,它运行良好!

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="XmlDataSource1">
  <ItemTemplate>
    <h3><%#XPath("Subject") %></h3>
    <p>Start: <%#XPath("StartDate") %>. End: <%#XPath("EndDate") %></p>
  </ItemTemplate>
</asp:Repeater>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="http://server.liveconnect.dk/xml/calendar/?cid=95&xid=e96de1864025d98d22b86537d02bb93e"></asp:XmlDataSource>

它不会显示数据。我担心的是 XML 中的 Info 元素会弄乱转发器。

4

2 回答 2

2

您需要在 XmlDataSource 上指定XPath选择器:

<asp:XmlDataSource XPath="Calendar/Appointment" ID="XmlDataSource1" runat="server" DataFile="http://server.liveconnect.dk/xml/calendar/?cid=95&xid=e96de1864025d98d22b86537d02bb93e"></asp:XmlDataSource>

得到正确的孩子。由于您在根日历下有不同的孩子,XmlDataSource 认为它应该使用第一个(信息),而您想使用其他的。

于 2013-09-11T16:24:53.583 回答
0

编辑:由于评论中提供了其他信息,此答案不正确。 接受的答案是正确的。

您的 XML 文件不包含您尝试显示的元素。

这是我从您的 XML中看到的内容:

<Calendar>
  <Info>
    <CalendarID>95</CalendarID>
    <CalendarName><![CDATA[Kalender Jebjerg]]></CalendarName>
    <AppointmentInterval>1 days</AppointmentInterval>
    <RefreshInterval>5 minutes</RefreshInterval>
    <FreeAccessUntil>2013-01-15 09:12:27</FreeAccessUntil>
    <NextExportAvailable>2013-09-11 18:04:41</NextExportAvailable>
  </Info>
</Calendar>

没有Subject,StartDateEndDate下的元素Info

例如,如果您修改代码以显示 XML 中的一些元素您将看到转发器工作正常:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="XmlDataSource1">
    <ItemTemplate>
        <h2><%#XPath("CalendarID") %></h2>
        <h3><%#XPath("Subject")%></h3>
        <p>Start: <%#XPath("StartDate")%>. End: <%#XPath("EndDate")%></p>
    </ItemTemplate>
</asp:Repeater>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="http://server.liveconnect.dk/xml/calendar/?cid=95&xid=e96de1864025d98d22b86537d02bb93e"></asp:XmlDataSource>
于 2013-09-11T16:08:00.027 回答