Method 1: Use the XML Control
Code
Copy codeThe Code is as follows:
<% @ Page Language = "C #" %>
<Html>
<Body>
<H3> <font face = "Verdana"> XML method 1 </font> <From runat = server>
<Asp: Xml id = "xml1" DocumentSource = "grade. xml" runat = "server"/>
</From>
</Body>
</Html>
Method 2: Use DOM technology
Code
Copy codeThe Code is as follows:
<% @ Page Language = "C #" %>
<% @ Import Namespace = "System. Xml" %>
<% @ Import Namespace = "System. Xml. Xsl" %>
<Html>
<Script language = "C #" runat = "server">
Void Page_Load (Object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument ();
Doc. Load (Server. MapPath ("grade. xml "));
Xml1.Document = doc;
}
</Script>
<Body>
<H3> <font face = "Verdana"> method 2 of XML reading </font> <From runat = server>
<Asp: Xml id = "xml1" runat = "server"/>
</From>
</Body>
</Html>
Method 3: Use a DataSet object
Code
Copy codeThe Code is as follows:
<% @ Page Language = "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 ("grade. xml "));
DgEmployees. DataSource = objDataSet. Tables ["student"]. DefaultView;
DgEmployees. DataBind ();
}
</Script>
<Body>
<H3> <font face = "Verdana"> XML reading method 3 </font> <Asp: DataGrid id = "dgEmployees" runat = "server"/>
</Body>
</Html>
Method 4: Read data by text
Code
Copy codeThe Code is as follows:
<% @ Page Language = "C #" %>
<% @ Import Namespace = "System. Xml" %>
<Html>
<Script language = "C #" runat = "server">
Private void Page_Load (Object sender, EventArgs e)
{
XmlTextReader objXMLReader = new XmlTextReader (Server. MapPath ("grade. xml "));
String strNodeResult = "";
XmlNodeType objNodeType;
While (objXMLReader. Read ())
{
ObjNodeType = objXMLReader. NodeType;
Swith (objNodeType)
{
Case XmlNodeType. XmlDeclaration:
// Read the XML File Header
StrNodeResult + = "XML Declaration: <B>" + objXMLReader. Name + "" + objXMLReader. Value + "</B> <br/> ";
Break;
Case XmlNodeType. Element:
// Read tags
StrNodeResult + = "Element: <B>" + objXMLReader. Name + "</B> <br/> ";
Break;
Case XmlNodeType. Text:
// Read value
StrNodeResult + = "-Value: <B>" + objXMLReader. Value + "</B> <br/> ";
Break;
}
// Determine whether the node has attributes
If (objXMLReader. AttributeCount> 0)
{// Judge all nodes cyclically
While (objXMLReader. movetonextatti.pdf)
{// Obtain the tag and Value
StrNodeResult + = "-Attribute: <B>" + objXMLReader. name + "</B> value: <B>" + objXMLReader. value + "</B> <br/> ";
}
}
LblFile. Text = strNodeResult;
}
}
</Script>
<Body>
<H3> <font face = "Verdana"> XML reading method 4 </font> <From runat = server>
<Asp: label id = "LblFile" runat = "server"/>
</From>
</Body>
</Html>