Asp. NET read XML file 4 methods of analysis _ practical skills

Source: Internet
Author: User
method One: Using an XML control
Code
Copy Code code as follows:

<% @ Page language= "C #"%>
<body>
<from runat=server>
<asp:xml id= "XML1" documentsource= "Grade.xml" runat= "Server"/>
</from>
</body>

method Two: Using DOM technology
Code
Copy Code code as follows:

<% @ Page language= "C #"%>
<% @ Import namespace= "System.Xml"%>
<% @ Import namespace= "SYSTEM.XML.XSL"%>
<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>
<from runat=server>
<asp:xml id= "XML1" runat= "Server"/>
</from>
</body>

method Three: Working with a DataSet object
Code
Copy Code code 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>
<asp:datagrid id= "dgemployees" runat= "Server"/>
</body>

method Four: read as text
Code
Copy Code code as follows:

<% @ Page language= "C #"%>
<% @ Import namespace= "System.Xml"%>
<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 XML file Headers
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 if the node has attributes
if (objxmlreader.attributecount>0)
{//All nodes are judged by loops
while (Objxmlreader.movetonextattibute)
{//Tag and value
strnoderesult+= "-attribute:<b>" +objxmlreader.name+ "</b> value:<b>" +objxmlreader.value+ "</b" ><br/> ";
}
}
Lblfile.text=strnoderesult;
}
}
</script>
<body>
<from runat=server>
<asp:label id= "lblfile" runat= "Server"/>
</from>
</body>
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.