XML 文档对象模型加载和使用XmlDocument (w3c DOM)

如何加载和使用 XmlDocument (W3C DOM)

此示例阐释实现下列规范的类:

DOM 是 XML 文档在内存中(缓存)的树形表示,它支持对文档进行浏览和编辑。

 
VB DOMInterfaceXmlDocument.aspx

[运行示例] | [查看源代码]

有许多作为 W3C DOM 的一部分实现的类。其中,XmlNode 类提供操作节点的方法和属性。其他许多 W3C 类是专门化的 XmlNode 类。XmlDocument 类表示 XML 文档,并且有一个 Load 方法,可从文件、流或 XmlReader 中加载文档。下面是这些类的列表。

派生来源
XmlNode 实现 ICloneable、IEnumerable
XmlDocument XmlNode
XmlElement XmlLinkedNode
XmlAttribute XmlNode
XmlCharacterData XmlLinkedNode
XmlText XmlCharacterData
XmlComment XmlCharacterData
XmlCDataSection XmlCharacterData
XmlWhitespace XmlCharacterData
XmlSignificantWhitespace XmlCharacterData
XmlProcessingInstruction XmlLinkedNode
XmlNodeList 实现 IEnumerable
XmlNamedNodeMap 实现 IEnumerable
XmlAttributeCollection XmlNamedNodeMap
XmlDocumentFragment XmlNode
XmlDocumentType XmlLinkedNode
XmlEntityReference XmlLinkedNode
XmlEntity XmlNode
XmlNotation XmlNode
XmlImplementation Object
XmlNodeChangedEventArgs EventArgs

下列代码阐释如何从 XmlTextReader 加载 XmlDocument,然后显示该文档的内容。也可从 URL 直接加载 XmlDocument,然后使用 XmlTextReader 读取该 XML 文档。

XmlTextReader reader = null;
            try
            {
            // Load the XML from file
            Console.WriteLine ("Reading file {0} ...", args);
            reader = new XmlTextReader (args);
            reader.WhitespaceHandling = WhitespaceHandling.None;
            Console.WriteLine ("File {0} read sucessfully ...", args);
            // Create an XmlDocument from the XmlTextReader
            XmlDocument myXmlDocument = new XmlDocument();
            myXmlDocument.Load (reader);
            Console.WriteLine ("XmlDocument loaded with XML data successfully ...");
            // Process the supplied XML file
            Console.WriteLine ("Processing ...");
            Console.WriteLine ();
            // Start from the document Element
            DisplayTree(myXmlDocument.DocumentElement);
            }
            catch (Exception e)
            {
            Console.WriteLine ("Exception: {0}", e.ToString());
            }
            finally
            {
            if (reader != null)
            reader.Close();
            }
            
C# VB  

为了定位该文档,DisplayTree 方法使用 XmlNode 的 HasChildNodes 和 FirstChild 属性递归地重复 XmlDocument,以沿着树向下移。NextSibling 属性移到紧靠在当前节点旁的节点,如果没有节点可移动,则返回一个空值。

public void DisplayTree(XmlNode node)
            {
            if (node != null)
            Format (node);
            if (node.HasChildNodes)
            {
            node = node.FirstChild;
            while (node != null)
            {
            DisplayTree(node);
            node = node.NextSibling;
            }
            }
            }
            
C# VB  

Name 属性给出节点的名称,Value 属性根据节点类型给出它的值。下表显示为给定的节点类型所返回的值。下列代码中所显示的 Format 方法使用 Name和 Value 属性显示当前节点的详细信息。

类型
属性 属性的值
CDATA CDATA 节的内容
Comment 注释的内容
Document 空引用(Visual Basic 中的 Nothing)
DocumentFragment 空引用(Visual Basic 中的 Nothing)
DocumentType 空引用(Visual Basic 中的 Nothing)
Element 空引用(Visual Basic 中的 Nothing)
Entity 空引用(Visual Basic 中的 Nothing)
EntityReference 空引用(Visual Basic 中的 Nothing)
Notation 空引用(Visual Basic 中的 Nothing)
ProcessingInstruction 除目标以外的全部内容
Text 文本节点的内容
XmlDeclaration 空引用(Visual Basic 中的 Nothing)

注意:XmlNode 类还具有 NodeType 属性。有关说明不同节点类型的表,请参阅如何从文件读取 XML

对于 XmlElement 节点类型,Attributes 属性提供一系列属性作为 XmlNamedNodeMap 类。XmlNameNodeMap 类还实现 IEnumerable,以支持 C# 中的 foreach 语句和 Visual Basic 中的 For Each…Next 语句。

// Format the output
            private void Format (XmlNode node)
            {
            if (!node.HasChildNodes)
            {
            Console.WriteLine("\t" + node.Name + "<" + node.Value + ">");
            }
            else
            {
            Console.Write(node.Name);
            if (XmlNodeType.Element == node.NodeType)
            {
            XmlNamedNodeMap map = node.Attributes;
            foreach (XmlNode attrnode in map)
            Console.Write(" " + attrnode.Name + "<" + attrnode.Value + "> ");
            }
            Console.WriteLine();
            }
            }
            
C# VB  

XmlNode 类还具有 NodeType 属性。请参阅如何从文件读取 XML节以获得不同节点类型的表。下面显示的是运行此示例所产生的输出。

Reading file books.xml ...
File books.xml read sucessfully ...
XmlDocument loaded with XML data successfully ...
Processing ...
bookstore
book genre<autobiography>  publicationdate<1981>  ISBN<1-861003-11-0>
title
#text<The Autobiography of Benjamin Franklin>
author
first-name
#text<Benjamin>
last-name
#text<Franklin>
price
#text<8.99>
book genre<novel>  publicationdate<1967>  ISBN<0-201-63361-2>
title
#text<The Confidence Man>
author
first-name
#text<Herman>
last-name
#text<Melville>
price
#text<11.99>
book genre<philosophy>  publicationdate<1991>  ISBN<1-861001-57-6>
title
#text<The Gorgias>
author
name
#text<Plato>
price
#text<9.99>

摘要

  1. XmlDocument、XmlNode 和其他类实现 W3C 文档对象模型级别 1 核心以及核心 DOM 级别 2 规范。
  2. XmlDocument 是 XML 文档在内存中(缓存)的树形表示。
  3. 有不同的从 XmlNode 专门化的节点类型,使您可以操作 XML 文档。
  4. 使用 XmlTextReader 和 XmlTextWriter 类,可以获得对 XML 的更快、非缓存和只进的流访问。
原文地址:https://www.cnblogs.com/chorrysky/p/584481.html