Read all XML node data using XmlDocument

Source: Internet
Author: User
Tags add foreach format count net return string tostring

There are many examples of asp.net reading XML, such as using a dataset to read, but this tutorial is using XmlDocument to read all the data under the XML node, let's look at this XML format first: Sysrightsdb.xml

XML Code
 
  <?xml version= "1.0" encoding= "Utf-8"?>
<root>
  <rights name= "SYS" >
    <xml name= "stock Quotes "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= "Reporting 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= "Application Audit" code= "So_audit_list" ></xml>
    <xml Name= "Permission list" code= "So_ur_sour_list" ></xml>
  </rights>  
</root>

From this XML you can see that I will use the ASP.net Read permission system the Node property equals all data under SYS and read all the data under the financial node.

This kind of XML data format makes it seem clear that you know what it means, and it looks good.

Let's start here.

1th, first create read XML class Xmlhepler, as follows:

Reference two namespaces first

Using System.Xml;
Using System.Collections;

Xmlhepler Code
 -->    <summary>///Author: Dodo///Web site: www.xueit.com//////Read XML classes///</summary>/// <typeparam name= "T" ></typeparam> public class Xmlhepler<t>:system.web.ui.page {HASHTABL
        e table = new Hashtable ();
        T FileName;    T Root; root node T rootattname; Node property name T Rootattvalue;       The root node attribute value T Field; XML field///<summary>///XML file path///</summary>///<param name= "Val" >&
            Lt;/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 Datat Able Getxmltodatatable () {string[] splitfield=field.tostring ().
            Split (",");
            Constructs a datatable datatable 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 child nodes under this node XmlNode List xnlist = XNode.
                    ChildNodes; Child node all data for (int i = 0; i < xnlist. Count; i)//for (int i = 0; i < XNode. Childnodes.count; I) This sentence is all XML child node data {DataRow dr = dt.
                        NewRow ();
                            The fields required for the binding are 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 Set Value///<summary>///root node///</summary> public T XmlRoot
            {get {return Root;}
        set {Root = value;}
            ///<summary>///node property field name///</summary> public T xmlrootattname {
            get {return rootattname;}
        set {rootattname = value;}
  }///<summary>///node property field values      </summary> public T Xmlrootattvalue {get {return rootattvalue;}
        set {Rootattvalue = value;}
            ///<summary>///child node property field///</summary> public T Xmlsplitfield {
        set {Field = value;} } #endregion}

This Xmlhepler class uses Documentelement.selectnodes to select an XML node, followed by the IF (XNode). Attributes[rootattname.tostring ()]. InnerText = = rootattvalue.tostring ()) Gets the node of the associated property.

Use the method getxmltodatatable () to construct the DataTable to import all the data under the XML node into the DataTable.

This kind of source code key place all wrote the annotation, very simple.

Let's take a look at how to invoke this class:

In the program Page_Load Test call Sysrightsdb.xml and show the relevant data.

The source code is as follows:

Call Xmlhepler Code
 -->protected void Page_Load (object sender, EventArgs e) {xmlhepler<string> xml = new Xmlhepler<string> (Serve
    R.mappath ("Sysrightsdb.xml"));
    Xml.xmlroot = "rights";
    Invokes the data Xml.xmlrootattname = "name" under the SYS node;
    Xml.xmlrootattvalue = "SYS";
    Xml.xmlsplitfield = "Code,name"; DataTable dt = XML.
    Getxmltodatatable ();
    Response.Write ("<b> system Permissions </b><br>"); foreach (DataRow dr in Dt. Rows) {Response.Write ("Name:" dr["name"]. ToString () "Code:" dr["code"].
        ToString ());
    Response.Write ("<br>");
    }//Call the financial node under the data Xml.xmlrootattname = "name";
    Xml.xmlrootattvalue = "financial";
    Xml.xmlsplitfield = "Code,name"; DT = XML.
    Getxmltodatatable ();
    Utils.response ("<b> subsystem Permissions </b><br>"); foreach (DataRow dr in Dt. Rows) {Response.Write ("Name:" dr["name"]. ToString () "Code:" dr["code"].
        ToString ());
    Response.Write ("<br>"); }
}

Well, to this point, it is very simple to implement the call XML node data, and finally look at the effect of the picture:

Tutorials are shorter, a lot of communication!



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.