|
An Introduction to Working with XML in .NET广告 An Introduction to
Working with XML in .NET Dan Wahlin 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); 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(); 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(); This code would result in the following XML document: <?xml version="1.0"? 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>"; 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 | 在线联系 |
CIO职场,强者生存?在2008年,我们将继续看到CIO向商业运营方向发展。与此同时,我们也会看到商业管理人员将与技术管理人员一起竞争CIO岗位。 IT领导者的就职机会虽有不少,但其难度将会大幅提高。2…… |
|
|