Use. NET read XML file

Source: Internet
Author: User
Tags add object net string
Xml
introduce

In this article I'll explain how to read XML files in a asp.net application, which is a very useful technique. With this technique, we can customize the configuration file for our application, or we can read the data stored in the XML file.

Introduction

The following code uses the XmlTextReader object to read data from the disk file into the XmlDocument object. XmlTextReader objects are functionally similar to StreamReader and BinaryReader objects, except that they are specifically designed to read XML files. In addition, there are some other XML-related features of the XmlTextReader object. For example, the WhitespaceHandling property used in the code tells the application not to establish nodes for extra spaces in the XML file.

The following code uses the DocumentElement property of the XmlTextReader object to find the root node of the tree representation of an XML document. The Addwithchildren method is then recursively called to add the node and its child nodes to the listbox.

The following code also contains the processing of the property. The attribute node is not included in the collection of child nodes of a XmlDocument object's nodes. Therefore, you can only use the XmlNode object's Attributes property to get the attribute node collection. After the collection of property nodes is obtained, the code uses the XmlNamedNodeMap object to save the collection. This object can hold any collection of XmlNode objects of any type.

code List



Program code:

private void Btnload_click (object sender, System.EventArgs e)

{

XmlTextReader reader = new XmlTextReader (

Server.MapPath ("Mycompany.xml"));

   

Reader. WhitespaceHandling = WhitespaceHandling.None;

XmlDocument xmldoc = new XmlDocument ();

To load a file into a XmlDocument object

Xmldoc.load (reader);

Close connection

Reader. Close ();

Add an element representing a document to the ListBox

LBNODES.ITEMS.ADD ("XML Document");

Finds the root node and adds it and its child nodes to the listbox

XmlNode Xnod = xmldoc.documentelement;

Addwithchildren (xnod,1);

}

  

private void Addwithchildren (XmlNode xnod, Int32 intlevel)

{

Add a node and its child nodes to the listbox

Intlevel to control indentation depth

XmlNode xnodworking;

String strindent = new String (', 2 * intlevel);

If the node has a value, read its value

String strvalue = (string) Xnod. Value;

if (strvalue!= null)

{

strvalue = ":" + strvalue;

}

Add the details of a node to the listbox

LBNODES.ITEMS.ADD (Strindent + xnod). Name + strvalue);

If it's an element node, get its properties

if (Xnod. NodeType = = XmlNodeType.Element)

{

XmlNamedNodeMap mapattributes = Xnod. Attributes;

To add a node property to the ListBox

foreach (XmlNode xnodattribute in mapattributes)

{

LBNODES.ITEMS.ADD (strindent + "" + Xnodattribute.name +

":" + xnodattribute.value);

}

If there are child nodes, call this program recursively

if (Xnod. HasChildNodes)

{

xnodworking = Xnod. FirstChild;

while (xnodworking!= null)

{

Addwithchildren (xnodworking, Intlevel + 1);

xnodworking = xnodworking.nextsibling;

}

}

}

}

}


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.