Read the XML file using .NET

zhaozj2021-02-16  59

Introduction

In this article, I will introduce how to read XML files in the ASP.NET application, which is a very useful technique. Using this trick, we can customize our application configuration files, or read those saved in the XML file.

Insteit

The following code will read the data in the disk file to the XMLDocument object using the XMLTextReader object. The XMLTextReader object is very similar to the StreamReader and BinaryReader objects, but it is specifically designed for reading the XML file. In addition, the XMLTextReader object also has some other features related to XML. For example, the WhitespaceHandLing property used in the code tells the application not to establish a node for extra spaces in the XML file.

The following code uses the XMLTextReader object's DocumentElement property to find the root node of the tree expression form of the XML document. After that, recursively call the AddWithchildren method to add nodes and its child nodes to listbox.

The following code also contains processing of attributes. The attribute node does not include a sub-node collection of a node of an XMLDocument object. Thus, you can only use the Attributes property of the XMLNode object to get the attribute node collection. After getting the property node collection, the code uses the XMLNameDNodeMap object to save this collection. This object can save any collection of any type of XMLNode object.

Code list

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 ();

// Load the file into the XMLDocument object

XMLDoc.Load (Reader);

// Close connection

Reader.Close ();

/ / Add an element that represents a document to ListBox

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

/ / Find the root node and add it and its child node to listbox

XMLNode XNOD = XMLDoc.documenTelement;

AddwithChildren (XNOD, 1);

}

Private void addwithchildren (XMLNode XNOD, INT32 INTLEVEL)

{

// Add the node and its child node to listbox

// intLevel controls the depth of indentation

XMLNode Xnodworking;

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

// If the node value value, read its value

String strign = (string) xnod.value;

IF (Strvalue! = NULL)

{

Strval = ":" Strvalue;

}

// Add the details of the node to ListBox

LBNODES.ITEMS.ADD (Strindent XNOD.Name Strvalue);

// If it is an element node, get its properties

IF (XNOD.NODETYPE == xmlnodetype.element)

{

XMLNameDnodeMap MapAttributes = XNOD.Attributes;

// Add node properties to ListBox

Foreach (XMLNode Xnotattribute In MaPattribute) {

lbnodes.Items.add (Strindent " XNODATTRIBUTE.NAME

":" XNODATTRIBUTE.VALUE);

}

// If there is a child node, recursively call this program.

IF (XNOD.HASCHILDNODES)

{

XnodWorking = Xnod.firstchild;

While (xnodworking! = null)

{

AddwithChildren (Xnodworking, Intlevel 1);

Xnodworking = xnodworking.nextsibling;

}

}

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-23425.html

New Post(0)