An Introduction to Working with XML in .NET

2002-9-19 9:19:30【作者】 畅享网 【进入论坛】
广告

An Introduction to Working with XML in .NET

Dan Wahlin

March 17, 2001

Level: Beginner

Working with XML in .NET applications is a fairly straightforward process especially if you've used MSXML3 in the past. Even if you haven't worked with MSXML3 you'll find that using the classes found in the .NET platform is easy once you know more about them.

There are two main APIs that you can use to access data found within XML documents. These include forward-only, non-cached access, and random access through using the Document Object Model (DOM). The classes that expose both APIs are found within the System.Xml assembly.

If you want to access data within an XML document in the most fast and efficient way possible then you'll want to use the XmlTextReader class. This class exposes a pull model that has many advantages over the push model found in the Simple API for XML (SAX). To use this class you must reference the System.Xml assembly in your file. In C# you would do this with the "using" keyword while in Visual Basic you would use the "imports" keyword. Once the assembly is referenced, you can then instantiate the reader as shown below:

XmlTextReader reader = new XmlTextReader(pathToXmlDoc);
   
int elementCount = 0;
   
while (reader.Read()) {
       
if (reader.NodeType == XmlNodeType.Element) {
           
elementCount++;
       
}
   
}

There are several different constructors available for the XmlTextReader class. The one shown above accepts an XML document file path as a string argument.

While the forward-only pull model is certainly very efficient, it is read-only and therefore doesn't allow you to insert, delete, or update nodes in a document. In cases where you need more control or flexibility over an XML document, you'll want to look at using the Document Object Model (DOM). The DOM API works by loading each node found within an XML document into a tree structure similar to a genealogical chart. By having this in-memory structure, random access to different nodes within an XML document is possible.

To start the process of creating the DOM tree, you need to reference the System.Xml assembly in your file and then instantiate the XmlDocument class:

XmlDocument xmlDoc = new XmlDocument();
   
xmlDoc.Load(pathToXmlDoc);
   
XmlNode root = xmlDoc.DocumentElement;

Adding nodes to the tree can easily be accomplished by using methods associated with the XmlDocument class. The following example shows how to load XML from a file and then add an element as a child of a root node named "root." It also shows how an attribute can be added to a node as well:

XmlDocument xmlDoc = new XmlDocument();
   
XmlDoc.Load(pathToXmlDoc);
   
XmlElement root = xmlDoc.DocumentElement;
   
XmlElement newNode = doc.CreateElement("newNode");
   
newNode.SetAttribute("id","1");
   
root.AppendChild(newNode);

This code would result in the following XML document:

<?xml version="1.0"?
   
<root>
       
<newNode id="1"/>
   
</root>

In cases where a string containing XML needs to be loaded into the DOM, the XmlDocument class's LoadXml() method can be used. Once in the DOM, the XML can then be manipulated as shown above:

string myXml = "<root><someNode>Hello</someNode></root>";
   
XmlDocument xmlDoc = new XmlDocument();
   
xmlDoc.LoadXml(myXml);
   
//....manipulation or reading of the nodes can occur here

There are many other classes that can perform a variety of tasks in the System.Xml assembly. This article has barely scratched the surface but has hopefully shown that much of your experience with MSXML3 can be used in .NET applications as well. Using many of the other XML related classes in the .NET platform will be detailed in future articles so stay tuned!

如果您希望与本文章的作者或其所在机构,进一步交流,请联系:畅享网 姜小姐
jill.jiang@amteam.org | 021-51096826-112 | 在线联系
老孙的IT运维管理之道[原创]用户的BSM用户的IT业务管..

从企业实际的IT运营角度来看,BSM是推动IT与业务融合,实现、改善WCNG司IT管理和治理的最佳实践之一。

吕建伟 专栏和CIO问答软件项目实施管理

现实中很少能按照正规流程来的,所以只能把流程中的各个环节拆开,个个击破,以后就可以见招拆招了。

节能与优化IT 企业CIO过冬良策

当前金融危机的影响还在继续漫延,很多企业都在苦寻过冬的良策,在这种情况下,节能与优化技术与产品无疑成为CIO们关注的首要对象,本次选题就是针对节能与优化IT来为CIO们提供过冬的良……

观08软件并购风潮 议09巨头何处生花

2008,似乎注定是不平静的一年。有人说2008是并购年。业内人士表示,在全球软件行业,并购一直是大企业谋求做大做强的捷径之一,包括甲骨文、SAP,微软等全球软件巨头都为了扩大自己……