Using a xmlDatasource in Code behind

Last post 05-16-2008 8:05 PM by anthonypj. 2 replies.

Sort Posts:

  • Using a xmlDatasource in Code behind

    05-16-2008, 3:49 AM
    • Member
      20 point Member
    • TokyoAnt
    • Member since 11-07-2005, 2:09 AM
    • Posts 85

    Hi, Can you use an XML Datasource in Code behind (say in the page load event) to bind to a repeater control? I try the code below but nothing shows up when I run the page

    This is the code behind:

     

    if (!Page.IsPostBack)
            {
                XmlDataSource xds = new XmlDataSource();
    
                xds.DataFile = "~/App_Data/test.xml";
                xds.XPath = "/NewDataSet/Customers[CustomerID = 'ALFKI']/Orders[Freight > 30]";
    
                repeaterXML.DataSource = xds;
            }

     and this is the source:

    <asp:Repeater ID="repeaterXML" runat="server">
            <HeaderTemplate><table><tr><td>Hello World</td></tr></HeaderTemplate>
            
            <ItemTemplate><tr><td><%# DataBinder.Eval(Container.DataItem,"Freight") %></td></tr></ItemTemplate>
            
            <FooterTemplate><tr><td>This is the footer</td></tr></FooterTemplate>
            
            </asp:Repeater>
     
    Any help would be most appreciated
    @nt
  • Re: Using a xmlDatasource in Code behind

    05-16-2008, 5:18 AM
    Answer
    • Contributor
      6,676 point Contributor
    • ramblor
    • Member since 03-13-2008, 10:03 AM
    • Posts 1,013

    You need to call repeaterXML.DataBind() I think.

    "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
  • Re: Using a xmlDatasource in Code behind

    05-16-2008, 8:05 PM
    Answer
    • Participant
      1,158 point Participant
    • anthonypj
    • Member since 05-11-2007, 11:31 AM
    • Posts 278

     You almost there .. look closely at the way data displayed in reapeter below ie <%# XPath("URL") %>

    Dim xds As XmlDataSource = New XmlDataSource()

    xds.XPath = "ROOT/ROW"

    xds.DataFile = "~/YourXML.xml"

    Repeater1.DataSource = xds

    Repeater1.DataBind()

    <asp:Repeater ID="Repeater1" runat="server" >

    <HeaderTemplate>

    <table>

    </HeaderTemplate>

    <ItemTemplate>

    <tr>

    <TD>

    <a href='<%# XPath("URL") %>' ><%#XPath("URL")%></a>

    </TD>

    <TD><%#XPath("Hits")%></TD>

    </TR>

    </ItemTemplate>

    <FooterTemplate>

    </table>

    </FooterTemplate>

    </asp:Repeater>

    <?xml version="1.0" encoding="utf-16" ?>]>

    <ROOT >

    <ROW>

    <URL>

    /main/depts.html

    </URL>

    <Hits>

    5872

    </Hits>

    </ROW>

    </ROOT>

Page 1 of 1 (3 items)