创建 XML 文档XML Document Creation

有两种创建 XML 文档的方法。There are two ways to create an XML document. 一种方法是,创建不含参数的 XmlDocument 。One way is to create an XmlDocument with no parameters. 另一种方法是,创建 XmlDocument ,并向它传递 XmlNameTable 参数。The other way is to create an XmlDocument and pass it an XmlNameTable as a parameter. 下面的示例展示了如何不使用任何参数新建空 XmlDocument 。The following example shows how to create a new, empty XmlDocument using no parameters.

Dim doc As New XmlDocument()  
XmlDocument doc = new XmlDocument();  

创建文档后,可以使用 Load 方法在文档中加载字符串、流、URL、文本读取器或 XmlReader 派生类中的数据。Once a document is created, you can load it with data from a string, stream, URL, text reader, or an XmlReader derived class using the Load method. 还有另一种加载方法 LoadXML 方法,可用于从字符串中读取 XML。There is also another load method, the LoadXML method, which reads XML from a string. 若要详细了解各种 Load 方法,请参阅将 XML 文档读取到 DOM 中For more information on the various Load methods, see Reading an XML Document into the DOM.

有一个类名为 XmlNameTable 。There is a class called the XmlNameTable. 此类是原子化字符串对象的表。This class is a table of atomized string objects. 该表使 XML 分析器可以高效地对 XML 文档中所有重复的元素和属性的名称使用相同的字符串对象。This table provides an efficient means for the XML parser to use the same string object for all repeated element and attribute names in an XML document. XmlNameTable 在文档创建(如上所示)时自动创建,并在文档加载时加载属性名和元素名称。An XmlNameTable is automatically created when a document is created as shown above and is loaded with attribute and element names when the document is loaded. 如果已有包含名称表的文档,且这些名称在另一个文档中很有用,可以使用需要使用 XmlNameTable 参数的 Load 方法新建文档。If you already have a document with a name table, and those names would be useful in another document, you can create a new document using the Load method that takes an XmlNameTable as a parameter. 使用此方法创建文档时,它使用现有 XmlNameTable ,其中包含已从其他文档加载到其中的所有属性和元素。When the document is created with this method, it uses the existing XmlNameTable with all the attributes and elements already loaded into it from the other document. 它可用于有效地比较元素和属性的名称。It can be used for efficiently comparing element and attribute names. 若要详细了解 XmlNameTable ,请参阅使用 XmlNameTable 比较对象For more information on the XmlNameTable, see Object Comparison Using XmlNameTable. 有关参考,请参阅 XmlNameTableFor reference, see XmlNameTable.

请参阅See also