XmlNamespaceManager.LookupNamespace 方法

获取指定前缀的命名空间 URI。

**命名空间:**System.Xml
**程序集:**System.Xml(在 system.xml.dll 中)

语法

声明
Public Overridable Function LookupNamespace ( _
    prefix As String _
) As String
用法
Dim instance As XmlNamespaceManager
Dim prefix As String
Dim returnValue As String

returnValue = instance.LookupNamespace(prefix)
public virtual string LookupNamespace (
    string prefix
)
public:
virtual String^ LookupNamespace (
    String^ prefix
)
public String LookupNamespace (
    String prefix
)
public function LookupNamespace (
    prefix : String
) : String

参数

  • prefix
    要解析其命名空间 URI 的前缀。若要匹配默认命名空间,请传递 String.Empty。

返回值

返回 prefix 的命名空间 URI;如果没有映射的命名空间,则为 空引用(在 Visual Basic 中为 Nothing)。返回的字符串是原子化的。 有关原子化字符串的更多信息,请参见 XmlNameTable

示例

下面的示例将前缀/命名空间对添加到 XmlNamespaceManager 中,然后显示集合中的所有对。

Option Explicit
Option Strict

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        Dim test As New Sample()
    End Sub 'Main
    
    Public Sub New()
        ' Create the XmlNamespaceManager.
        Dim nt As New NameTable()
        Dim nsmgr As New XmlNamespaceManager(nt)
        
        ' Add prefix/namespace pairs to the XmlNamespaceManager.
        nsmgr.AddNamespace("", "www.wideworldimporters.com") 'Adds a default namespace.
        nsmgr.AddNamespace("europe", "www.wideworldimporters.com/europe")
        nsmgr.PushScope() 'Pushes a namespace scope on the stack.
        nsmgr.AddNamespace("", "www.lucernepublishing.com") 'Adds another default namespace.
        nsmgr.AddNamespace("partners", "www.lucernepublishing.com/partners")
        
        Console.WriteLine("Show all the prefix/namespace pairs in the XmlNamespaceManager...")
        ShowAllNamespaces(nsmgr)
    End Sub 'New
    
    
    Private Sub ShowAllNamespaces(nsmgr As XmlNamespaceManager)
        Do
            Dim prefix As String
            For Each prefix In  nsmgr
                Console.WriteLine("Prefix={0}, Namespace={1}", prefix, nsmgr.LookupNamespace(prefix))
            Next prefix
        Loop While nsmgr.PopScope()
    End Sub 'ShowAllNamespaces
End Class 'Sample
using System;
using System.IO;
using System.Xml;


public class Sample
{
  public static void Main()
  {
    Sample test = new Sample();
  }
  public Sample()
  {
    // Create the XmlNamespaceManager.
    NameTable nt = new NameTable();
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);

    // Add prefix/namespace pairs to the XmlNamespaceManager.
    nsmgr.AddNamespace("", "www.wideworldimporters.com"); //Adds a default namespace.
    nsmgr.AddNamespace("europe", "www.wideworldimporters.com/europe");
    nsmgr.PushScope();  //Pushes a namespace scope on the stack.
    nsmgr.AddNamespace("", "www.lucernepublishing.com"); //Adds another default namespace.
    nsmgr.AddNamespace("partners", "www.lucernepublishing.com/partners");

    Console.WriteLine("Show all the prefix/namespace pairs in the XmlNamespaceManager...");
    ShowAllNamespaces(nsmgr);
  }

  private void ShowAllNamespaces(XmlNamespaceManager nsmgr)
  {
    do{
       foreach (String prefix in nsmgr)
       {
        Console.WriteLine("Prefix={0}, Namespace={1}", prefix,nsmgr.LookupNamespace(prefix));
       } 
    }
    while (nsmgr.PopScope());
  }
}
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
public ref class Sample
{
public:
   Sample()
   {
      
      // Create the XmlNamespaceManager.
      NameTable^ nt = gcnew NameTable;
      XmlNamespaceManager^ nsmgr = gcnew XmlNamespaceManager( nt );
      
      // Add prefix/namespace pairs to the XmlNamespaceManager.
      nsmgr->AddNamespace( "", "www.wideworldimporters.com" ); //Adds a default namespace.
      nsmgr->AddNamespace( "europe", "www.wideworldimporters.com/europe" );
      nsmgr->PushScope(); //Pushes a namespace scope on the stack.
      nsmgr->AddNamespace( "", "www.lucernepublishing.com" ); //Adds another default namespace.
      nsmgr->AddNamespace( "partners", "www.lucernepublishing.com/partners" );
      Console::WriteLine( "Show all the prefix/namespace pairs in the XmlNamespaceManager..." );
      ShowAllNamespaces( nsmgr );
   }


private:
   void ShowAllNamespaces( XmlNamespaceManager^ nsmgr )
   {
      do
      {
         System::Collections::IEnumerator^ myEnum = nsmgr->GetEnumerator();
         while ( myEnum->MoveNext() )
         {
            String^ prefix = safe_cast<String^>(myEnum->Current);
            Console::WriteLine( "Prefix={0}, Namespace={1}", prefix, nsmgr->LookupNamespace( prefix ) );
         }
      }
      while ( nsmgr->PopScope() );
   }

};

int main()
{
   gcnew Sample;
}
import System.*;
import System.IO.*;
import System.Xml.*;

public class Sample
{
    public static void main(String[] args)
    {
        Sample test = new Sample();
    } //main

    public Sample()
    {
        // Create the XmlNamespaceManager.
        NameTable nT = new NameTable();
        XmlNamespaceManager nsMgr = new XmlNamespaceManager(nT);

        // Add prefix/namespace pairs to the XmlNamespaceManager.
        //Adds a default namespace.
        nsMgr.AddNamespace("", "www.wideworldimporters.com"); 
        nsMgr.AddNamespace("europe", "www.wideworldimporters.com/europe");
        nsMgr.PushScope(); //Pushes a namespace scope on the stack.

        //Adds another default namespace.
        nsMgr.AddNamespace("", "www.lucernepublishing.com"); 
        nsMgr.AddNamespace("partners", "www.lucernepublishing.com/partners");
        Console.WriteLine("Show all the prefix/namespace pairs in the " 
            + "XmlNamespaceManager...");
        ShowAllNamespaces(nsMgr);
    } //Sample

    private void ShowAllNamespaces(XmlNamespaceManager nsMgr)
    {
        do {
            String prefix;
            System.Collections.IEnumerator objEnum = nsMgr.GetEnumerator();
            while (objEnum.MoveNext()) {
                prefix = objEnum.get_Current().ToString();
                Console.WriteLine("Prefix={0}, Namespace={1}", prefix, 
                    nsMgr.LookupNamespace(prefix));
            }
        } while (nsMgr.PopScope());
    } //ShowAllNamespaces
} //Sample

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

XmlNamespaceManager 类
XmlNamespaceManager 成员
System.Xml 命名空间