Repeater and XMLDataSource

Last post 07-03-2008 7:11 AM by Samu Zhang - MSFT. 8 replies.

Sort Posts:

  • Repeater and XMLDataSource

    06-27-2008, 10:27 AM
    • Loading...
    • Andrew-Ita
    • Joined on 05-27-2008, 2:10 PM
    • Posts 31

     Hi, I have this code:

     

    <asp:XmlDataSource runat="server" id="xmlDS" XPath="envelope" >
              <Data>        
                <envelope>
                    <header>
                        <version>1.5.1</version>
                        <timestamp>20080627150909</timestamp>
                    </header>
                    <response type="details" product="hotel">
                        <shop value="false"/>
                        <golf value="true"/>
                        <radio value="false"/>
                    </response>
              </envelope>
           </Data>
      </asp:XmlDataSource>
                  
        <asp:repeater ID="repeater1" runat="server" DataSourceID="xmlDS" >
            <ItemTemplate>
                <asp:Label ID="value" Runat="server" Text='<%# XPath("response/*/@value") %>' /><br /> 
            </ItemTemplate>
        </asp:repeater>

     As you can see in the XPath expression, I want repeater to write the value attribute of each children of response element. But it writes only the first value!! (the "shop" value).

    What's wrong in the XPath expression?

    Thanks 

     


     

  • Re: Repeater and XMLDataSource

    06-27-2008, 11:07 AM
    • Loading...
    • drocco
    • Joined on 03-25-2008, 1:39 AM
    • Posts 358

    Is there any reason you can't use 3 separate Label controls?

     

    <asp:Label ID="shopLbl" Runat="server" Text='<%# XPath("response/shop/@value") %>' />
    <asp:Label ID="golfLbl" Runat="server" Text='<%# XPath("response/golf/@value") %>' />
    <asp:Label ID="radioLbl" Runat="server" Text='<%# XPath("response/radio/@value") %>' />
     
    If that's no good you should probably explain how you would like the result to look 
     
  • Re: Repeater and XMLDataSource

    06-27-2008, 11:24 AM
    • Loading...
    • Andrew-Ita
    • Joined on 05-27-2008, 2:10 PM
    • Posts 31

    drocco:

    Is there any reason you can't use 3 separate Label controls?

     

    <asp:Label ID="shopLbl" Runat="server" Text='<%# XPath("response/shop/@value") %>' />
    <asp:Label ID="golfLbl" Runat="server" Text='<%# XPath("response/golf/@value") %>' />
    <asp:Label ID="radioLbl" Runat="server" Text='<%# XPath("response/radio/@value") %>' />
     
    If that's no good you should probably explain how you would like the result to look 
     

     

     

    I could do that, but response children are almost 60! So I think it's better to write only one label (and make it repeat by the repeater) rather then write 60 label :)

  • Re: Repeater and XMLDataSource

    06-27-2008, 11:46 AM
    • Loading...
    • drocco
    • Joined on 03-25-2008, 1:39 AM
    • Posts 358

    Well the reason I asked you to specify how you would want the result to look/be formatted is because you can construct a value for the label programatically, that is a combination of those 3 XML attributes... but how would you want it to look ie: "shop, golf, radio, "

    It sounds to me like what you're trying to accomplish would be the simplest through a GridView where you can just use Bound Fields and specify that attribute that each field is for

    IE:

             <asp:GridView ID="GridView1" runat="server" DataSourceID="xmlDS">
                <Columns>
                    <asp:BoundField DataField="response/shop" HeaderText="Shop" />

                    <asp:BoundField DataField="response/golf" HeaderText="Golf" />
                   

                    etc
                    etc
                </Columns>
            </asp:GridView>

  • Re: Repeater and XMLDataSource

    06-27-2008, 11:56 AM
    • Loading...
    • drocco
    • Joined on 03-25-2008, 1:39 AM
    • Posts 358

    Actually scratch that, you can do it this way to make it super easy

           

            <asp:DataList ID="DataList1" runat="server" DataSourceID="xmlDS">
                <ItemTemplate>
                    <table>
                        <tr>
                            <td>
                                <%#XPath("response/shop/@value")%>
                            </td>
                            <td>
                                <%#XPath("response/golf/@value")%>
                            </td>
                            <td>
                                <%#XPath("response/radio/@value")%>
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:DataList>
            <asp:XmlDataSource runat="server" ID="xmlDS" XPath="envelope">
                <Data>    
                    <envelope>
                    <header>
                        <version>1.5.1</version>
                        <timestamp>20080627150909</timestamp>
                    </header>
                    <response type="details" product="hotel">
                        <shop value="false"/>
                        <golf value="true"/>
                        <radio value="false"/>
                    </response>
              </envelope>
                </Data>
            </asp:XmlDataSource>

  • Re: Repeater and XMLDataSource

    06-27-2008, 12:10 PM
    • Loading...
    • Andrew-Ita
    • Joined on 05-27-2008, 2:10 PM
    • Posts 31

     Ok, thanks drocco :)

    Now I'd like to visualize only the elements which have "true" in their value attribute, so, for example, I'd like to have this output:

    golf

    Beacuse golf is the only one with true value.

    PS: Is there any way to change the visualization of the element? For example instead of "golf" I would visualize "we have golf"

    So the FINAL issue is: take the elements which have attribute value="true" and, if possible, change their name at runtime.
     

  • Re: Repeater and XMLDataSource

    06-27-2008, 12:17 PM
    • Loading...
    • drocco
    • Joined on 03-25-2008, 1:39 AM
    • Posts 358

    I think I have a better understanding of what you're try to do now

    You're making 60 elements and you just want to use the ones that have a value of true so you're just inflating the size of your XML file when you may be better off just making a <description>Short description of the hotel</description> and calling it that way

    In other words, you might think of other ways to get a similar result because this might be more trouble than it's worth

    #1 you don't want to have We have this on the page 60 times and if you have We Have followed by a string of what they do have, it may not be grammatically correct... We golf, shop, radio or We have a golf, shop, radio doesn't make sense 

     

  • Re: Repeater and XMLDataSource

    06-27-2008, 12:47 PM
    • Loading...
    • drocco
    • Joined on 03-25-2008, 1:39 AM
    • Posts 358

     Bump

  • Re: Repeater and XMLDataSource

    07-03-2008, 7:11 AM
    Answer

    Hi Andrew-Ita,

    You can write some code.

     

       protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void DataList1_Load(object sender, EventArgs e)
        {
    
        }
        protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            PlaceHolder ph = e.Item.FindControl("PlaceHolder1") as PlaceHolder;
            
            if (ph != null)
            {
                
                IXPathNavigable nav = e.Item.DataItem as IXPathNavigable;
                XPathNavigator xnav = nav.CreateNavigator();
                XPathNodeIterator iter =  xnav.Select("response/*");
                while (iter.MoveNext())
                {
                    string str = iter.Current.Name;
                    XPathNavigator attnav =  iter.Current.CreateNavigator();
                    attnav.MoveToFirstAttribute();
                    string atti = attnav.Value;
                    if (atti == "true")
                    {
                        LiteralControl liter = new LiteralControl();
                        liter.Text = str + "is ok";
                        ph.Controls.Add(liter);
                    }
                }
            }
        }
      
        <form id="form1" runat="server">
            <div>
                <asp:DataList ID="DataList1" runat="server" DataSourceID="xmlDS" OnItemDataBound="DataList1_ItemDataBound" OnLoad="DataList1_Load">
                    <ItemTemplate>
                       <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
                    </ItemTemplate>
                </asp:DataList>
                <asp:XmlDataSource runat="server" ID="xmlDS" XPath="envelope">
                    <Data>     
                    <envelope>
                    <header>
                        <version>1.5.1</version>
                        <timestamp>20080627150909</timestamp>
                    </header>
                    <response type="details" product="hotel">
                        <shop value="false"/>
                        <golf value="true"/>
                        <radio value="false"/>
                    </response>
              </envelope>
                    </Data>
                </asp:XmlDataSource>
            </div>
        </form>

     

     

    Sincerely,
    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
Page 1 of 1 (9 items)
Microsoft Communities
Page view counter