locked
read live xml / rss feed and save then display in gridview RRS feed

  • Question

  • User-1994971408 posted

    Hi , 

     I want to make an xml File from live xml / rss  feed ... means i have one link like  "https://s3.amazonaws.com/GSTArticles/GoogleMaps/Landmarks.xml

    so how to i save this xml file into our project using c# code  and also how to display some specific column of this file in gried view .. needed for rss feed also ..then i will manage it for sorting ,paging etc..

    thanks in advance ..

    Monday, January 16, 2012 8:49 AM

Answers

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 16, 2012 10:06 AM
  • User3866881 posted

    so how to i save this xml file into our project using c# code  and also how to display some specific column of this file in gried view .. needed for rss feed also ..then i will manage it for sorting ,paging etc..

    Addition to smirnov:)

    If your xml looks like this,you can just use a DataSet.WriteToXml/ReadToXml to deal with your problem——Now I did a sample for you:

    【xml contents】

    <?xml version="1.0" standalone="yes"?>
    <DocumentElement>
    <book id="bk105">
    <author>Corets, Eva</author>
    <title>The Sundered Grail</title>
    <genre>Fantasy</genre>
    <price>5.95</price>
    <publish_date>2001-09-10</publish_date>
    <description>
    The two daughters of Maeve, half-sisters,
    battle one another for control of England. Sequel to
    Oberon's Legacy.
    </description>
    </book>
    <book id="bk107">
    <author>Thurman, Paula</author>
    <title>Splish Splash</title>
    <genre>Romance</genre>
    <price>4.95</price>
    <publish_date>2000-11-02</publish_date>
    <description>
    A deep sea diver finds true love twenty
    thousand leagues beneath the sea.
    </description>
    </book>
    <book id="bk108">
    <author>Knorr, Stefan</author>
    <title>Creepy Crawlies</title>
    <genre>Horror</genre>
    <price>4.95</price>
    <publish_date>2000-12-06</publish_date>
    <description>
    An anthology of horror stories about roaches,
    centipedes, scorpions and other insects.
    </description>
    </book>
    <book id="bk109">
    <author>Kress, Peter</author>
    <title>Paradox Lost</title>
    <genre>Science Fiction</genre>
    <price>6.95</price>
    <publish_date>2000-11-02</publish_date>
    <description>
    After an inadvertant trip through a Heisenberg
    Uncertainty Device, James Salway discovers the problems
    of being quantum.
    </description>
    </book>
    <book id="bk110">
    <author>O'Brien, Tim</author>
    <title>Microsoft .NET: The Programming Bible</title>
    <genre>Computer</genre>
    <price>36.95</price>
    <publish_date>2000-12-09</publish_date>
    <description>
    Microsoft's .NET initiative is explored in
    detail in this deep programmer's reference.
    </description>
    </book>
    <book id="bk111">
    <author>O'Brien, Tim</author>
    <title>MSXML3: A Comprehensive Guide</title>
    <genre>Computer</genre>
    <price>36.95</price>
    <publish_date>2000-12-01</publish_date>
    <description>
    The Microsoft MSXML3 parser is covered in
    detail, with attention to XML DOM interfaces, XSLT processing,
    SAX and more.
    </description>
    </book>
    <book id="bk112">
    <author>Galos, Mike</author>
    <title>Visual Studio 7: A Comprehensive Guide</title>
    <genre>Computer</genre>
    <price>49.95</price>
    <publish_date>2001-04-16</publish_date>
    <description>
    Microsoft Visual Studio 7 is explored in depth,
    looking at how Visual Basic, Visual C++, C#, and ASP+ are
    integrated into a comprehensive development
    environment.
    </description>
    </book>
    </DocumentElement>
    【aspx codes】
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="XmlEditInsertDeleteInGridView.Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Xml-based CRUD Code Sample</title>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.min.js"></script>
    <script type="text/javascript">


    $(function () {

    // Find each row of the GridView
    $("tr").each(function () {

    // Find each table cell of the row and find the second linkbutton,
    // Add the confirm dialog before deleting.
    $(this).find("td:eq(0)>a:eq(1)").click(function () {

    return confirm("Are you sure to delete?");
    });

    });
    })
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <h2>
    Xml-based CRUD Code Sample——</h2>
    <asp:GridView ID="GridView1" runat="server" Width="70%" AutoGenerateDeleteButton="True"
    AutoGenerateEditButton="True" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None"
    BorderWidth="1px" CellPadding="3" CellSpacing="2" OnRowCancelingEdit="GridView1_RowCancelingEdit"
    OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting">
    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
    <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
    <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
    <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#FFF1D4" />
    <SortedAscendingHeaderStyle BackColor="#B95C30" />
    <SortedDescendingCellStyle BackColor="#F1E5CE" />
    <SortedDescendingHeaderStyle BackColor="#93451F" />
    </asp:GridView>
    </div>
    <p>
    Id:<asp:TextBox ID="tbId" runat="server"></asp:TextBox>
    </p>
    <p>
    author:<asp:TextBox ID="tbAuthor" runat="server"></asp:TextBox>
    </p>
    <p>
    title:<asp:TextBox ID="tbTitle" runat="server"></asp:TextBox>
    </p>
    <p>
    genre:<asp:TextBox ID="tbGenre" runat="server"></asp:TextBox>
    </p>
    <p>
    price:<asp:TextBox ID="tbPrice" runat="server"></asp:TextBox>
    </p>
    <p>
    publishdate:<asp:TextBox ID="tbPublishDate" runat="server"></asp:TextBox>
    </p>
    <p>
    description:<asp:TextBox ID="tbDescription" runat="server"></asp:TextBox>
    </p>
    <asp:Button ID="btnInsert" runat="server" Height="26px" OnClick="btnInsert_Click"
    Text="Insert" Width="141px" />
    </form>
    </body>
    </html>
    【cs codes】
    /****************************** Module Header ******************************\
    * Module Name: Default.aspx.cs
    * Project: CSASPNETCRUDXmlInGridView
    * Copyright (c) Microsoft Corporation
    *
    * The project shows up how to insert/delete/update a record into the xml file
    * by the GridView.
    *
    * This source is subject to the Microsoft Public License.
    * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
    * All other rights reserved.
    \***************************************************************************/

    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;

    namespace XmlEditInsertDeleteInGridView
    {
    public partial class Default : System.Web.UI.Page
    {
    /// <summary>
    /// For the first time when the page loads, load data into DataTable
    /// with DataSet, and save the DataTable into ViewState for further
    /// usage.
    /// </summary>
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    DataSet ds = new DataSet();
    ds.ReadXml(Request.MapPath("try.xml"));
    GridView1.DataSource = ds.Tables[0];
    GridView1.DataBind();
    ViewState["dt"] = ds.Tables[0];
    }
    }

    /// <summary>
    /// Handle the Edit event of GridView for assigning the specific row to be
    /// in the edit mode.
    /// </summary>
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
    GridView1.EditIndex = e.NewEditIndex;
    GridView1.DataSource = (DataTable)ViewState["dt"];
    GridView1.DataBind();
    }

    /// <summary>
    /// Update the specific row in the DataTable with the data from GridView,
    /// re-write the data into the xml file and re-databind again.
    /// </summary>
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    DataTable dt = (DataTable)ViewState["dt"];

    for (int i = 1; i < GridView1.Rows[e.RowIndex].Cells.Count; i++)
    {
    dt.Rows[e.RowIndex][i-1] = (GridView1.Rows[e.RowIndex].Cells[i].Controls[0] as TextBox).Text;
    }
    dt.AcceptChanges();
    GridView1.EditIndex = -1;
    GridView1.DataSource = dt;
    GridView1.DataBind();
    dt.WriteXml(Request.MapPath("try.xml"));
    }

    /// <summary>
    /// Cancel edit and set the mode of the GridView to normal viewing mode.
    /// </summary>
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
    GridView1.EditIndex = -1;
    DataTable dt = (DataTable)ViewState["dt"];
    GridView1.DataSource = dt;
    GridView1.DataBind();
    }

    /// <summary>
    /// Insert the data into the DataTable, re-write into the xml file and
    /// re-databind to the GridView.
    /// </summary>
    protected void btnInsert_Click(object sender, EventArgs e)
    {
    DataTable dt = (DataTable)ViewState["dt"];
    dt.Rows.Add(tbAuthor.Text, tbTitle.Text, tbGenre.Text, tbPrice.Text, tbPublishDate.Text, tbDescription.Text, tbId.Text);
    dt.AcceptChanges();
    dt.WriteXml(Request.MapPath("try.xml"));
    GridView1.DataSource = dt;
    GridView1.DataBind();
    }

    /// <summary>
    /// Delete the row from DataTable and write data into xml file,
    /// re-databind to the GridView.
    /// </summary>
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
    DataTable dt = (DataTable)ViewState["dt"];
    dt.Rows.RemoveAt(e.RowIndex);
    dt.WriteXml(Request.MapPath("try.xml"));
    GridView1.DataSource = dt;
    GridView1.DataBind();
    }
    }
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 17, 2012 8:49 PM

All replies

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 16, 2012 10:06 AM
  • User3866881 posted

    so how to i save this xml file into our project using c# code  and also how to display some specific column of this file in gried view .. needed for rss feed also ..then i will manage it for sorting ,paging etc..

    Addition to smirnov:)

    If your xml looks like this,you can just use a DataSet.WriteToXml/ReadToXml to deal with your problem——Now I did a sample for you:

    【xml contents】

    <?xml version="1.0" standalone="yes"?>
    <DocumentElement>
    <book id="bk105">
    <author>Corets, Eva</author>
    <title>The Sundered Grail</title>
    <genre>Fantasy</genre>
    <price>5.95</price>
    <publish_date>2001-09-10</publish_date>
    <description>
    The two daughters of Maeve, half-sisters,
    battle one another for control of England. Sequel to
    Oberon's Legacy.
    </description>
    </book>
    <book id="bk107">
    <author>Thurman, Paula</author>
    <title>Splish Splash</title>
    <genre>Romance</genre>
    <price>4.95</price>
    <publish_date>2000-11-02</publish_date>
    <description>
    A deep sea diver finds true love twenty
    thousand leagues beneath the sea.
    </description>
    </book>
    <book id="bk108">
    <author>Knorr, Stefan</author>
    <title>Creepy Crawlies</title>
    <genre>Horror</genre>
    <price>4.95</price>
    <publish_date>2000-12-06</publish_date>
    <description>
    An anthology of horror stories about roaches,
    centipedes, scorpions and other insects.
    </description>
    </book>
    <book id="bk109">
    <author>Kress, Peter</author>
    <title>Paradox Lost</title>
    <genre>Science Fiction</genre>
    <price>6.95</price>
    <publish_date>2000-11-02</publish_date>
    <description>
    After an inadvertant trip through a Heisenberg
    Uncertainty Device, James Salway discovers the problems
    of being quantum.
    </description>
    </book>
    <book id="bk110">
    <author>O'Brien, Tim</author>
    <title>Microsoft .NET: The Programming Bible</title>
    <genre>Computer</genre>
    <price>36.95</price>
    <publish_date>2000-12-09</publish_date>
    <description>
    Microsoft's .NET initiative is explored in
    detail in this deep programmer's reference.
    </description>
    </book>
    <book id="bk111">
    <author>O'Brien, Tim</author>
    <title>MSXML3: A Comprehensive Guide</title>
    <genre>Computer</genre>
    <price>36.95</price>
    <publish_date>2000-12-01</publish_date>
    <description>
    The Microsoft MSXML3 parser is covered in
    detail, with attention to XML DOM interfaces, XSLT processing,
    SAX and more.
    </description>
    </book>
    <book id="bk112">
    <author>Galos, Mike</author>
    <title>Visual Studio 7: A Comprehensive Guide</title>
    <genre>Computer</genre>
    <price>49.95</price>
    <publish_date>2001-04-16</publish_date>
    <description>
    Microsoft Visual Studio 7 is explored in depth,
    looking at how Visual Basic, Visual C++, C#, and ASP+ are
    integrated into a comprehensive development
    environment.
    </description>
    </book>
    </DocumentElement>
    【aspx codes】
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="XmlEditInsertDeleteInGridView.Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Xml-based CRUD Code Sample</title>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.min.js"></script>
    <script type="text/javascript">


    $(function () {

    // Find each row of the GridView
    $("tr").each(function () {

    // Find each table cell of the row and find the second linkbutton,
    // Add the confirm dialog before deleting.
    $(this).find("td:eq(0)>a:eq(1)").click(function () {

    return confirm("Are you sure to delete?");
    });

    });
    })
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <h2>
    Xml-based CRUD Code Sample——</h2>
    <asp:GridView ID="GridView1" runat="server" Width="70%" AutoGenerateDeleteButton="True"
    AutoGenerateEditButton="True" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None"
    BorderWidth="1px" CellPadding="3" CellSpacing="2" OnRowCancelingEdit="GridView1_RowCancelingEdit"
    OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting">
    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
    <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
    <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
    <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#FFF1D4" />
    <SortedAscendingHeaderStyle BackColor="#B95C30" />
    <SortedDescendingCellStyle BackColor="#F1E5CE" />
    <SortedDescendingHeaderStyle BackColor="#93451F" />
    </asp:GridView>
    </div>
    <p>
    Id:<asp:TextBox ID="tbId" runat="server"></asp:TextBox>
    </p>
    <p>
    author:<asp:TextBox ID="tbAuthor" runat="server"></asp:TextBox>
    </p>
    <p>
    title:<asp:TextBox ID="tbTitle" runat="server"></asp:TextBox>
    </p>
    <p>
    genre:<asp:TextBox ID="tbGenre" runat="server"></asp:TextBox>
    </p>
    <p>
    price:<asp:TextBox ID="tbPrice" runat="server"></asp:TextBox>
    </p>
    <p>
    publishdate:<asp:TextBox ID="tbPublishDate" runat="server"></asp:TextBox>
    </p>
    <p>
    description:<asp:TextBox ID="tbDescription" runat="server"></asp:TextBox>
    </p>
    <asp:Button ID="btnInsert" runat="server" Height="26px" OnClick="btnInsert_Click"
    Text="Insert" Width="141px" />
    </form>
    </body>
    </html>
    【cs codes】
    /****************************** Module Header ******************************\
    * Module Name: Default.aspx.cs
    * Project: CSASPNETCRUDXmlInGridView
    * Copyright (c) Microsoft Corporation
    *
    * The project shows up how to insert/delete/update a record into the xml file
    * by the GridView.
    *
    * This source is subject to the Microsoft Public License.
    * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
    * All other rights reserved.
    \***************************************************************************/

    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;

    namespace XmlEditInsertDeleteInGridView
    {
    public partial class Default : System.Web.UI.Page
    {
    /// <summary>
    /// For the first time when the page loads, load data into DataTable
    /// with DataSet, and save the DataTable into ViewState for further
    /// usage.
    /// </summary>
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    DataSet ds = new DataSet();
    ds.ReadXml(Request.MapPath("try.xml"));
    GridView1.DataSource = ds.Tables[0];
    GridView1.DataBind();
    ViewState["dt"] = ds.Tables[0];
    }
    }

    /// <summary>
    /// Handle the Edit event of GridView for assigning the specific row to be
    /// in the edit mode.
    /// </summary>
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
    GridView1.EditIndex = e.NewEditIndex;
    GridView1.DataSource = (DataTable)ViewState["dt"];
    GridView1.DataBind();
    }

    /// <summary>
    /// Update the specific row in the DataTable with the data from GridView,
    /// re-write the data into the xml file and re-databind again.
    /// </summary>
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    DataTable dt = (DataTable)ViewState["dt"];

    for (int i = 1; i < GridView1.Rows[e.RowIndex].Cells.Count; i++)
    {
    dt.Rows[e.RowIndex][i-1] = (GridView1.Rows[e.RowIndex].Cells[i].Controls[0] as TextBox).Text;
    }
    dt.AcceptChanges();
    GridView1.EditIndex = -1;
    GridView1.DataSource = dt;
    GridView1.DataBind();
    dt.WriteXml(Request.MapPath("try.xml"));
    }

    /// <summary>
    /// Cancel edit and set the mode of the GridView to normal viewing mode.
    /// </summary>
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
    GridView1.EditIndex = -1;
    DataTable dt = (DataTable)ViewState["dt"];
    GridView1.DataSource = dt;
    GridView1.DataBind();
    }

    /// <summary>
    /// Insert the data into the DataTable, re-write into the xml file and
    /// re-databind to the GridView.
    /// </summary>
    protected void btnInsert_Click(object sender, EventArgs e)
    {
    DataTable dt = (DataTable)ViewState["dt"];
    dt.Rows.Add(tbAuthor.Text, tbTitle.Text, tbGenre.Text, tbPrice.Text, tbPublishDate.Text, tbDescription.Text, tbId.Text);
    dt.AcceptChanges();
    dt.WriteXml(Request.MapPath("try.xml"));
    GridView1.DataSource = dt;
    GridView1.DataBind();
    }

    /// <summary>
    /// Delete the row from DataTable and write data into xml file,
    /// re-databind to the GridView.
    /// </summary>
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
    DataTable dt = (DataTable)ViewState["dt"];
    dt.Rows.RemoveAt(e.RowIndex);
    dt.WriteXml(Request.MapPath("try.xml"));
    GridView1.DataSource = dt;
    GridView1.DataBind();
    }
    }
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 17, 2012 8:49 PM