Four methods and sample code for reading XML file information in ASP. NET

Source: Internet
Author: User

Method 1: Use the XML Control
<% @ 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
<% @ 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
<% @ 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
<% @ 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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.