There are a lot of ASP online. NET to read XML, for example, using Dataset to read, but this tutorial uses XmlDocument to read all data under the XML node. Let's take a look at the XML format: SysRightsDb. xml
XML Code [http://www.xueit.com]
<? Xml version = "1.0" encoding = "UTF-8"?> <Root> <rights name = "SYS"> <xml name = "stock market" code = "stockmarket"> </xml> <xml name = "Business System" code =" sales "> </xml> <xml name =" customer service system "code =" servcice "> </xml> <xml name =" financial system "code =" financial "> </xml> <xml name = "Call Center System" code = "callcenter"> </xml> <xml name = "report system" code = "report"> </xml> </rights> <rights name = "financial"> <xml name = "Order Management" code = "so_mana"> </xml> <xml name = "Order List" code = "so_list"> </xml> <xml name = "apply for audit" code = "so_audit_list"> </xml> <xml name = "permission list" code = "so_ur_sour_list"> </xml> </rights> </root>
From this XML, I will use ASP. NET to read all the data under SYS and all the data under the financial node.
This XML data format makes people clearly understand what it means and looks good.
Let's start.
1st first, create the xmlHepler class for reading the XML file. The content is as follows:
Reference two namespaces first
Using System. Xml;
Using System. Collections;
XmlHepler Code [http://www.xueit.com]
/// <Summary> /// Author: dodo /// Website: www.xueit.com // read the XML class // </summary> /// <typeparam name = "T"> </typeparam> public class xmlHepler <T>: system. web. UI. page {Hashtable table = new Hashtable (); T FileName; T Root; // Root node T RootAttName; // node attribute name T RootAttValue; // Root node attribute value T Field; // Xml field // <summary> // XML file path // </summary> /// <param name = "val"> </param> public xmlHepler (T val) {FileName = val; this. loadXml (val. toString ());} /// <summary> /// XML file path // </summary> /// <param name = "file"> </param> private void LoadXml (string file) {XmlDocument xdoc = new XmlDocument (); xdoc. load (file); table. add ("xml", xdoc );} /// <summary> /// return XML to DataTable /// </summary> /// <returns> </returns> public DataTable GetXmlToDataTable () {string [] SplitField = Field. toString (). split (','); // construct the DataTable dt able dt = new DataTable (); DataColumn dc = null; for (int I = 0; I <SplitField. length; I) {dc = new DataColumn (SplitField [I]); dt. columns. add (dc);} XmlDocument xdoc = (XmlDocument) table ["xml"]; XmlNodeList xTable = xdoc. documentElement. selectNodes (Root. toString (); foreach (XmlNode xnode in xTable) {if (xnode. attributes [RootAttName. toString ()]. innerText = RootAttValue. toString () // a node {// All subnodes under this node XmlNodeList xnlist = xnode. childNodes; // all data of the subnode for (int I = 0; I <xnlist. count; I) // for (int I = 0; I <xnode. childNodes. count; I) this statement is the data of all xml subnodes {DataRow dr = dt. newRow (); // bind the required field for (int j = 0; j <SplitField. length; j) {dr [SplitField [j] = xnode. childNodes [I]. attributes [SplitField [j]. value;} dt. rows. add (dr) ;}}return dt ;} # region setting value // <summary> // Root node // </summary> public T xmlRoot {get {return Root;} set {Root = value ;}} /// <summary> // name of the node attribute field /// </summary> public T xmlRootAttName {get {return RootAttName;} set {RootAttName = value ;}} /// <summary> // node attribute field value /// </summary> public T xmlRootAttValue {get {return RootAttValue;} set {RootAttValue = value ;}} /// <summary> /// subnode attribute Field /// </summary> public T xmlSplitField {set {Field = value ;}# endregion}
The xmlHepler class uses DocumentElement. selectNodes to select the XML node, and then if (xnode. attributes [RootAttName. toString ()]. innerText = RootAttValue. toString.
Use GetXmlToDataTable () to construct a able and import all data under the XML node to the DataTable.
The key points of the source code of this class are annotated, which is very simple.
Let's take a look at how to call this class:
Call SysRightsDb. xml in the program Page_Load test and display the relevant data.
The source code is as follows:
Call xmlHepler Code [http://www.xueit.com]
Protected void Page_Load (object sender, EventArgs e) {xmlHepler <string> xml = new xmlHepler <string> (Server. mapPath ("SysRightsDb. xml "); xml. xmlRoot = "rights"; // call the data xml under the SYS node. xmlRootAttName = "name"; xml. xmlRootAttValue = "SYS"; xml. xmlSplitField = "code, name"; DataTable dt = xml. getXmlToDataTable (); Response. write ("<B> system permission </B> <br>"); foreach (DataRow dr in dt. rows) {Response. write ("name:" dr ["name"]. toString () "code:" dr ["code"]. toString (); Response. write ("<br>");} // call the data xml under the financial node. xmlRootAttName = "name"; xml. xmlRootAttValue = "financial"; xml. xmlSplitField = "code, name"; dt = xml. getXmlToDataTable (); Utils. response ("<B> subsystem permission </B> <br>"); foreach (DataRow dr in dt. rows) {Response. write ("name:" dr ["name"]. toString () "code:" dr ["code"]. toString (); Response. write ("<br> ");}}
Well, now, it's easy to call the XML node data. Finally, let's take a look:
The tutorial is short and has a lot of communication!