Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show hierarchical xml data using ASP.NET Grid

I have the following XML data. I need to present them on ASP.NET web page in a hierarchical tabular format.

XML:

<Developers>
  <Region name="UK">
    <Region name="England">
      <Region name="London">
        <Data Date="01-01-2019">
          <Value name="DotNet">100</Value>
        </Data>
        <Data Date="01-01-2020">
          <Value name="DotNet">200</Value>
          <Value name="Java">300</Value>
        </Data>
      </Region>
      <Region name="Other">
        <Data Date="01-01-2019">
          <Value name="DotNet">400</Value>
        </Data>
        <Data Date="01-06-2019">
          <Value name="DotNet">500</Value>
        </Data>
      </Region>
    </Region>
    <Region name="Scotland">
      <Data Date="01-01-2019">
        <Value name="DotNet">600</Value>
      </Data>
    </Region>
  </Region>
  <Region name="France">
    <Data Date="01-06-2020">
      <Value name="DotNet">700</Value>
    </Data>
  </Region>
  <Region name="Germany">
    <Data Date="01-06-2019">
      <Value name="Java">800</Value>
    </Data>
  </Region>
</Developers>

Expected Output:

enter image description here

where (-) is tree view's expand/collapse control.

Is it possible to achieve this using ASP.NET Data Grid? Any code example would be really useful.

like image 696
developer Avatar asked Aug 23 '19 10:08

developer


1 Answers

In the asp.net grid view to display the xml data.

Default asp web page

<html>
  <head>
    <title>Datagrid With XML </title>
    <link rel="stylesheet" href="css/ASPNetCookbook.css">
  </head>
  <body leftmargin="0" marginheight="0" marginwidth="0" topmargin="0">
    <form id="frmDatagrid" method="post" runat="server">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td align="center">
            <img src="images/ASPNETCookbookHeading_blue.gif">
          </td>
        </tr>
        <tr>
          <td class="dividerLine">
            <img src="images/spacer.gif" height="6" border="0"></td>
        </tr>
      </table>
      <table width="90%" align="center" border="0">
        <tr>
          <td><img src="images/spacer.gif" height="10" border="0"></td>
        </tr>
        <tr>
          <td align="center" class="PageHeading">
             DataGrid Using Data From XML (VB)</td>
        </tr>
        <tr>
          <td><img src="images/spacer.gif" height="10" border="0"></td>
        </tr>
        <tr>
          <td align="center">
            <asp:DataGrid 
                                id="dgRegion" 
                                runat="server" 
                                BorderColor="000080" 
                                BorderWidth="2px"
                                AutoGenerateColumns="False"
                                width="100%">

                                <HeaderStyle 
                                  HorizontalAlign="Center" 
                                  ForeColor="#FFFFFF" 
                                  BackColor="#000080" 
                                  Font-Bold=true
                                  CssClass="TableHeader" /> 

                                <ItemStyle
                                  BackColor="#FFFFE0" 
                                  cssClass="TableCellNormal" />

                                <AlternatingItemStyle 
                                  BackColor="#FFFFFF" 
                                  cssClass="TableCellAlternating" />


                                <Columns>
                                  <asp:BoundColumn HeaderText="Region" DataField="Region" />
                                  <asp:BoundColumn HeaderText="Data " DataField="Data " 
                                                   ItemStyle-HorizontalAlign="Center" />
                                  <asp:BoundColumn HeaderText="Value " DataField="Value "
                                                   ItemStyle-HorizontalAlign="Center" />
                                </Columns>
                              </asp:DataGrid>
          </td>
        </tr>
      </table>
    </form>
</body>
</html>

In the cs code is given below.

using System;
using System.Configuration;
using System.Data;
using System.Data.OleDb;

namespace ASPNetCookbook.CSExamples
{
  public class CH01DataGridWithXMLCS : System.Web.UI.Page
  {

    private void Page_Load(object sender, System.EventArgs e)
    {
      const String Region_TABLE = "Region";

      DataSet dSet = null;
      String xmlFilename = null;

      if (!Page.IsPostBack)
      {
        try
        {
          // get fully qualified path to the "Region" xml document located

                            xmlFilename = Server.MapPath("xml") + "\\Region.xml";


                            dSet = new DataSet( );
                            dSet.ReadXml(xmlFilename);

                            // bind the dataset to the datagrid
                            dgRegion.DataSource = dSet.Tables[Region_TABLE];
                           dgRegion.DataBind( );
        }  

        finally
        {
          // cleanup
          if (dSet != null)
          {
            dSet.Dispose( );
          }
        }  // finally
      }
    }  // Page_Load
  }  
}
like image 57
Bibin Avatar answered Oct 17 '22 19:10

Bibin