XmlNamespaceManager.LookupNamespace メソッド

指定したプリフィックスの名前空間 URI を取得します。

Public Overridable Function LookupNamespace( _
   ByVal prefix As String _) As String
[C#]
public virtual string LookupNamespace(stringprefix);
[C++]
public: virtual String* LookupNamespace(String* prefix);
[JScript]
public function LookupNamespace(
   prefix : String) : String;

パラメータ

  • prefix
    解決する対象となる名前空間 URI を持つプリフィックス。既定の名前空間に一致するようにするには、String.Empty を渡します。

戻り値

prefix の名前空間 URI を返します。マップされた名前空間がない場合は null 参照 (Visual Basic では Nothing) を返します。返される文字列は最小単位に分割されます。

最小単位に分割された文字列の詳細については、 XmlNameTable のトピックを参照してください。

使用例

[Visual Basic, C#, C++] プリフィックスと名前空間のペアを 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

[C#] 
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());
  }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

public __gc class Sample
{
public:
  Sample()
  {
    // Create the XmlNamespaceManager.
    NameTable* nt = new NameTable();
    XmlNamespaceManager* nsmgr = new XmlNamespaceManager(nt);

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

    Console::WriteLine(S"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 = __try_cast<String*>(myEnum->Current);
        Console::WriteLine(S"Prefix={0}, Namespace={1}",prefix,nsmgr->LookupNamespace(prefix));
       } 
    }
    while (nsmgr->PopScope());
  }
};

int main()
{
  new Sample();
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

XmlNamespaceManager クラス | XmlNamespaceManager メンバ | System.Xml 名前空間