XmlNamespaceManager(XmlNameTable) 构造函数

定义

使用指定的 XmlNamespaceManager 初始化 XmlNameTable 类的新实例。Initializes a new instance of the XmlNamespaceManager class with the specified XmlNameTable.

public:
 XmlNamespaceManager(System::Xml::XmlNameTable ^ nameTable);
public XmlNamespaceManager (System.Xml.XmlNameTable nameTable);
new System.Xml.XmlNamespaceManager : System.Xml.XmlNameTable -> System.Xml.XmlNamespaceManager
Public Sub New (nameTable As XmlNameTable)

参数

nameTable
XmlNameTable

要使用的 XmlNameTableThe XmlNameTable to use.

例外

向构造函数传递了 nullnull is passed to the constructor

示例

下面的示例 XmlNamespaceManager 使用读取器的名称表创建一个。The following example creates an XmlNamespaceManager using the name table of the reader.

      XmlTextReader^ reader = gcnew XmlTextReader( "myfile.xml" );
      XmlNamespaceManager^ nsmanager = gcnew XmlNamespaceManager( reader->NameTable );
      nsmanager->AddNamespace( "msbooks", "www.microsoft.com/books" );
      nsmanager->PushScope();
      nsmanager->AddNamespace( "msstore", "www.microsoft.com/store" );
      while ( reader->Read() )
      {
         Console::WriteLine( "Reader Prefix:{0}", reader->Prefix );
         Console::WriteLine( "XmlNamespaceManager Prefix:{0}", nsmanager->LookupPrefix( nsmanager->NameTable->Get( reader->NamespaceURI ) ) );
      }
   }

};

XmlTextReader reader = new XmlTextReader("myfile.xml");
XmlNamespaceManager nsmanager = new XmlNamespaceManager(reader.NameTable);
nsmanager.AddNamespace("msbooks", "www.microsoft.com/books");
nsmanager.PushScope();
nsmanager.AddNamespace("msstore", "www.microsoft.com/store");
while (reader.Read())
{
    Console.WriteLine("Reader Prefix:{0}", reader.Prefix);
    Console.WriteLine("XmlNamespaceManager Prefix:{0}",
    nsmanager.LookupPrefix(nsmanager.NameTable.Get(reader.NamespaceURI)));
}
Dim reader As New XmlTextReader("myfile.xml")
Dim nsmanager As New XmlNamespaceManager(reader.NameTable)
nsmanager.AddNamespace("msbooks", "www.microsoft.com/books")
nsmanager.PushScope()
nsmanager.AddNamespace("msstore", "www.microsoft.com/store")
While reader.Read()
    Console.WriteLine("Reader Prefix:{0}", reader.Prefix)
    Console.WriteLine("XmlNamespaceManager Prefix:{0}",             nsmanager.LookupPrefix(nsmanager.NameTable.Get(reader.NamespaceURI)))
End While

注解

名称表用于查找前缀和命名空间。The name table is used to look up prefixes and namespaces. 可在构造函数中指定具有预先原子化字符串的现有名称表。An existing name table with pre-atomized strings can be specified in the constructor. 这样做有几个优点。There are several advantages in doing so. 例如,如果使用对象的名称表 XmlReader ,则在每次读取后,任何推送到名称表中的命名空间和前缀字符串都可以由重复使用 XmlNamespaceManagerFor example, if the name table of an XmlReader object is used, after each read, any namespace and prefix strings pushed into the name table can be re-used by XmlNamespaceManager.

有关原子化字符串的详细信息,请参阅 XmlNameTableFor more information on atomized strings, see XmlNameTable.

备注

如果指定现有名称表,则不会将名称表中的任何命名空间自动添加到中 XmlNamespaceManagerIf you specify an existing name table, any namespaces in the name table are not automatically added to XmlNamespaceManager. 必须使用 AddNamespaceRemoveNamespace 来添加或删除命名空间。You must use AddNamespace and RemoveNamespace to add or remove namespaces.

适用于