ASP.Net/XML/XML DataGrid

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Bind data in XML to asp:DataGrid

<%@ Page language="c#" %>
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Xml" %>
<script language="c#" runat="server">
  void Page_Load()
  { 
//    string xmlFilename = @"C:\artists.xml";
    string xmlFilename = MapPath("AdRotatorBasic.xml");
    DataSet newDataSet = new DataSet(); 
    newDataSet.ReadXml(xmlFilename);
    DataGrid1.DataSource = newDataSet;
    DataGrid1.DataBind();
  }
</script>
<html>
  <head>
    <title>Data Grid Control example</title>
  </head>
  <body>
    <asp:DataGrid id="DataGrid1" runat="server"  />
  </body>
</html>



Bind XML file to DataSet and bind to asp datagrid

<%@ Import namespace="System.Data" %>
<html>
  <head>
    <title>Read XML file</title>
  </head>
  <body>
    <H2>Read XML file</H2>
      <asp:Label id="lblXMLFileName" runat="server" /><BR/><BR/>
      <asp:DataGrid id="dgServers" runat="server" />
  </body>
</html>
<script language="VB" runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
  Dim strXMLFile As String = MapPath("Multiple_levels.XML")
  lblXMLFileName.Text = strXMLFile
  Dim objDataSet As New DataSet()
  objDataSet.ReadXml(strXMLFile)
  dgServers.DataSource = objDataSet.Tables(0).DefaultView
  dgServers.DataBind()
End Sub
</script>
<%--
<?xml version="1.0" standalone="yes"?>
<CarList>
  <Car>
    <ModelName>A</ModelName>
    <Doors>2</Doors>
    <ColorList>
      <Color>Color 2</Color>
      <Color>Color 3</Color>
      <Color>Color 4</Color>
    </ColorList>
  </Car>
  <Car>
    <ModelName>Model 2</ModelName>
    <Doors>3</Doors>
    <ColorList>
      <Color>Red</Color>
      <Color>Yellow</Color>
      <Color>Blue</Color>
    </ColorList>
  </Car>
</CarList>
--%>



DataSet read XML data and bind to asp datagrid

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script language="VB" runat="server">
  Sub Page_Load(Sender As Object, E As EventArgs)
    Dim objDataSet    As New DataSet()
    objDataSet.ReadXml(Server.MapPath("NewEmployees.xml"))
    
    dgEmployees.DataSource = objDataSet.Tables(0).DefaultView
    dgEmployees.DataBind()
    
  End Sub
</script>
<html>
  <body>
  <asp:DataGrid id="dgEmployees" runat="server" />
  </body>
</html>

<%--NewEmployees.xml
<?xml version="1.0"?>
<employees>
  <employee id="1">    
      <firstName>Nancy</firstName>
      <lastName>Lee</lastName> 
    <city>Seattle</city>
    <state>WA</state>
    <zipCode>98122</zipCode>   
  </employee>
  <employee id="2">    
      <firstName>Jason</firstName>
      <lastName>Wang</lastName>
    <city>Vancouver</city>
    <state>WA</state>
    <zipCode>98123</zipCode>   
  </employee> 
</employees>
--%>



Load XML to asp:DataGrid

<%@ Page language="VB" runat="server" %>
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.XML" %>
<script language="vb" runat="server">
  Sub Page_Load()
    Dim xmlFilename As String 
    xmlFilename= "artists.xml"
    Dim newDataSet As New DataSet
    newDataSet.ReadXML(xmlFilename)
    DataGrid1.DataSource = newDataSet
    DataGrid1.DataBind()
  End Sub
</script>
<html>
  <head>
    <title>Data Grid Control example</title>
  </head>
  <body>
    <asp:DataGrid id="DataGrid1" runat="server"  />
  </body>
</html>

<%--
<?xml version="1.0"?>
<artist>
  <item>
    <name>Vincent Van Gogh</name>
    <nationality>Dutch</nationality>
    <movement>Post Impressionism </movement>
    <birthdate>30th March 1853</birthdate>
  </item>
  <item>
    <name>Paul Klee </name>
    <nationality>Swiss </nationality>
    <movement>Abstract Expressionism </movement>
    <birthdate>18th December 1879</birthdate>
  </item>
  <item>
    <name>Max Ernst </name>
    <nationality>German </nationality>
    <movement>Surrealism </movement>
    <birthdate>2nd April 1891</birthdate>
  </item>
</artist>

--%>



Read xml data and bind to datagrid in C#

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script Language="c#" runat="server">
  void Page_Load(object sender, EventArgs e)
  {
    DataSet objDataSet = new DataSet();
    objDataSet.ReadXml(Server.MapPath("NewEmployees.xml"));
    dgEmployees.DataSource = objDataSet.Tables[0].DefaultView;
    dgEmployees.DataBind();
  }
</script>
<html>
  <body>
  <asp:DataGrid id="dgEmployees" runat="server" />
  </body>
</html>
<%--NewEmployees.xml
<?xml version="1.0"?>
<employees>
  <employee id="1">    
      <firstName>Nancy</firstName>
      <lastName>Lee</lastName> 
    <city>Seattle</city>
    <state>WA</state>
    <zipCode>98122</zipCode>   
  </employee>
  <employee id="2">    
      <firstName>Jason</firstName>
      <lastName>Wang</lastName>
    <city>Vancouver</city>
    <state>WA</state>
    <zipCode>98123</zipCode>   
  </employee> 
</employees>
--%>