简体   繁体   中英

Listview to XML in asp.net

I need to write my listview data into XML file and download it.

my listview like as follows

<asp:ListView ID="lstBrand" runat="server">
        <LayoutTemplate>
            <table>
                <thead>
                    <tr>
                        <th>
                            S.No.
                        </th>
                        <th>
                            Category Name
                        </th>
                     </tr>
                </thead>
                <tbody>
                    <asp:PlaceHolder ID="ItemPlaceHolder" runat="server"></asp:PlaceHolder>
                </tbody>
            </table>
        </LayoutTemplate>
        <EmptyDataTemplate>
            <asp:Label ID="Label" runat="server" Text="No Records Found" ForeColor="Red" Font-Bold="true"></asp:Label>
        </EmptyDataTemplate>
        <ItemTemplate>
            <tr>
                <td align="center" width="30px">
                    <asp:Label ID="lblIndex" runat="server" Text='<%# Container.DataItemIndex + 1 %>' />
                </td>
                <td>
                    <asp:Label ID="lblBrandName" runat="server" Text='<%#Eval("Name") %>' />
                </td>
            </tr>
        </ItemTemplate>
    </asp:ListView>

Here my .cs

string str = "attachment; filename=" + xmlfileName + ".xml";
        currentPage.Response.ClearContent();
        currentPage.Response.AddHeader("content-disposition", str);
        currentPage.Response.ContentType = "application/xml";
        StringWriter writer = new StringWriter();
        HtmlTextWriter writer2 = new HtmlTextWriter(writer);
        HtmlForm child = new HtmlForm();
        lstBrand.Parent.Controls.Add(child);
        child.Attributes["runat"] = "server";
        child.Controls.Add(lstBrand);
        child.RenderControl(writer2);
        currentPage.Response.Write(writer.ToString());
        currentPage.Response.End();

Its export the listview sourcecode as it is. How to export my listview data into xml.

You will need to write is as an xml document.

Create a new xml document:

XmlDocument doc = new XmlDocument();

append your data to the document and save it.

Use this as reference: Inserting data into XmlDoument

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM