ASP.Net/XML/XML DataSet
Версия от 15:30, 26 мая 2010; (обсуждение)
Содержание
Display column information: dataset from xml file
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SqlClient" %>
<html>
<head>
<title>Display Column Information</title>
</head>
</html>
<script language="VB" runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
" Create dataset and data adapter with properties that apply to all tables
Dim objDataSet As New DataSet("EmployeePage")
" First Table - "Comments Table" From XML
objDataSet.ReadXmlSchema(Server.MapPath("Comments.xsd"))
objDataSet.ReadXml(Server.MapPath("Comments.xml"))
" Diagnostic print of tables in objDataSet - loop through DataSet.Tables
Dim strNames As String
Dim c As DataColumn
Dim iTableItem As DataTable
For Each iTableItem In objdataSet.Tables
strNames &= "Table Name: " & iTableItem.tableName & "<br/>"
For Each c In iTableItem.Columns
strNames &= "- Column " & c.ColumnName & " is of type " _
& c.DataType.ToString & "<br/>"
Next
Next
Response.Write(strNames)
End Sub
</script>
<%-- Comments.xsd
<?xml version="1.0" standalone="yes"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema">
<element name="Reviews">
<complexType>
<choice maxOccurs="unbounded">
<element name="Review">
<complexType>
<sequence>
<element name="ReviewID" type="int" />
<element name="ProductName" type="string" />
<element name="EmployeeID" type="int" />
<element name="Date" type="date" />
<element name="Comment" type="string" />
</sequence>
</complexType>
</element>
</choice>
</complexType>
</element>
</schema>
--%>
<%-- Comments.xml
<?xml version="1.0" standalone="yes"?>
<Reviews>
<Review>
<ReviewID>1</ReviewID>
<ProductName>Name</ProductName>
<EmployeeID>6</EmployeeID>
<Date>2001-01-01</Date>
<Comment>
comment
</Comment>
</Review>
</Reviews>
--%>
Load XML data into DataSet
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Configuration"%>
<%@ Import Namespace="System.Data"%>
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
DataSet authorsDataSet;
string filePath = Server.MapPath("Authors.xml");
authorsDataSet = new DataSet();
//Read the contents of the XML file into the DataSet
authorsDataSet.ReadXml(filePath);
authorsGird.DataSource = authorsDataSet.Tables[0].DefaultView;
authorsGird.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Reading XML Data into a DataSet object </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView id="authorsGird" runat="server"
AutoGenerateColumns="False" CellPadding="4" HeaderStyle-BackColor="blue" HeaderStyle-ForeColor="White"
HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True">
<Columns>
<asp:BoundField HeaderText="Last Name" DataField="lastName" />
<asp:BoundField HeaderText="First Name"
DataField="firstName" ItemStyle-HorizontalAlign="Right" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Load xml data to DataSet
<%@ 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("Employees.xml"))
dgEmployees.DataSource = objDataSet.Tables(0).DefaultView
dgEmployees.DataBind()
End Sub
</script>
<html>
<body>
<asp:DataGrid id="dgEmployees" runat="server" />
</body>
</html>
Modify XML data set
<%@ 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()
" read in the XML file
objDataSet.ReadXml(Server.MapPath("NewEmployees.xml"))
" show it in a grid
dgEmployees1.DataSource = objDataSet.Tables(0).DefaultView
dgEmployees1.DataBind()
" modify a row
objDataSet.Tables("employee").Rows(0).Item("firstName") = "Bob"
objDataSet.Tables("employee").Rows(0).Item("lastName") = "Dylan"
" add a new row to the table
Dim objTable As DataTable
Dim objNewRow As DataRow
objTable = objDataSet.Tables("employee")
objNewRow = objTable.NewRow()
objNewRow.Item("firstName") = "Norman"
objNewRow.Item("lastName") = "Blake"
objTable.Rows.Add(objNewRow)
" save it to a new file
objDataSet.WriteXml(Server.MapPath("Employees2.xml"))
" read in the new file
Dim objDataSet2 As New DataSet()
objDataSet2.ReadXml(Server.MapPath("Employees2.xml"))
" show it in another grid
dgEmployees2.DataSource = objDataSet2.Tables(0).DefaultView
dgEmployees2.DataBind()
End Sub
</script>
<html>
<body>
<table>
<tr>
<td valign="top"><asp:DataGrid id="dgEmployees1" runat="server" /></td>
<td valign="top"><asp:DataGrid id="dgEmployees2" runat="server" /></td>
</tr>
</table>
</body>
</html>
<%--
<?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>
--%>