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@amt.com.cn | 021-51096826-112 | 在线联系
吕建伟 专栏CRM下午茶(四):销售成熟度与..

经销商现在都精细化销售了,细节化销售的CRM,对企业销售帮助不明显了,真正的CRM就该上场了。 

周宏钧的信息化之路[原创]信息化助力企业脱身金融..

从上下游整合的角度看,信息化是一种策略工具和有效手段,信息系统平台提供管控平台,加速整合过程,降低整合风险。

第二届中国管理软件与IT服务年会—2..

“第二届中国管理软件与IT服务年会”于2008年7月23日-25日举行,由AMT集团与畅享网共同主办,无锡扬名高新技术产业园特别赞助支持。

CIO职场,强者生存?

在2008年,我们将继续看到CIO向商业运营方向发展。与此同时,我们也会看到商业管理人员将与技术管理人员一起竞争CIO岗位。 IT领导者的就职机会虽有不少,但其难度将会大幅提高。2……