次の方法で共有


XPathExpression.SetContext メソッド

名前空間を解決するために使用する XmlNamespaceManager を指定します。

Public MustOverride Sub SetContext( _
   ByVal nsManager As XmlNamespaceManager _)
[C#]
public abstract void SetContext(XmlNamespaceManagernsManager);
[C++]
public: virtual void SetContext(XmlNamespaceManager* nsManager) = 0;
[JScript]
public abstract function SetContext(
   nsManager : XmlNamespaceManager);

パラメータ

  • nsManager
    名前空間を解決するために使用する XmlNamespaceManager オブジェクト。

例外

例外の種類 条件
XPathException nsManagerXmlNamespaceManager から派生していません。

解説

名前空間の解決は、プリフィックスと名前空間 URI のマップを格納している XmlNamespaceManager を使用してサポートされます。XPathExpression で名前空間の解決が必要な場合、プリフィックスおよび名前空間 URI ペアを XmlNamespaceManager に追加し、 SetContext メソッドを呼び出して、名前空間の解決に使用する XmlNamespaceManager を指定する必要があります。

メモ    XPathExpression にプリフィックスが含まれていない場合、名前空間 URI は、空の名前空間であると見なされます。XML に既定の名前空間が含まれている場合は、まだ SetContext メソッドを使用し、プリフィックスと名前空間 URI を含んでいる XmlNamespaceManager を提供して、既定の名前空間を処理する必要があります。

たとえば、次の XML がある場合を示します。

<bookstore xmlns="http://www.lucernepublishing.com">
 <book>
 <title>Pride And Prejudice</title>
 </book>
</bookstore>

すべての book ノードを選択する C# コードを次に示します。

XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");
XPathExpression expr;
expr = nav.Compile("//ab:book");
expr.SetContext(nsmgr);
XPathNodeIterator iterator = nav.Select(expr);

使用例

[Visual Basic, C#, C++] すべての ISBN 属性を表示する例を次に示します。

 
Option Explicit
Option Strict

Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.XPath

Public Class Sample
   
   Public Shared Sub Main()
      
      Dim doc As New XPathDocument("booksort.xml")
      Dim nav As XPathNavigator = doc.CreateNavigator()
      
      'Select all ISBN attributes.
      Dim expr As XPathExpression
      expr = nav.Compile("/bookstore/book/@bk:ISBN")
      
      Dim nsmgr As New XmlNamespaceManager(nav.NameTable)
      nsmgr.AddNamespace("bk", "urn:samples")
      expr.SetContext(nsmgr)
      
      'Display the selection.
      Dim iterator As XPathNodeIterator = nav.Select(expr)
      While iterator.MoveNext()
         Console.WriteLine(iterator.Current.ToString())
      End While
   End Sub 'Main
End Class 'Sample 

[C#] 
using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;

public class Sample
{
  public static void Main()
  {

      XPathDocument doc = new XPathDocument("booksort.xml");
      XPathNavigator nav = doc.CreateNavigator();

      //Select all ISBN attributes.
      XPathExpression expr; 
      expr = nav.Compile("/bookstore/book/@bk:ISBN");
 
      XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
      nsmgr.AddNamespace("bk", "urn:samples");
      expr.SetContext(nsmgr);

      //Display the selection.
      XPathNodeIterator iterator = nav.Select(expr);
      while (iterator.MoveNext()){
        Console.WriteLine(iterator.Current.ToString());
      }
   }

}

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

int main()
{

   XPathDocument* doc = new XPathDocument(S"booksort.xml");
   XPathNavigator* nav = doc->CreateNavigator();

   //Select all ISBN attributes.
   XPathExpression* expr; 
   expr = nav->Compile(S"/bookstore/book/@bk:ISBN");

   XmlNamespaceManager* nsmgr = new XmlNamespaceManager(nav->NameTable);
   nsmgr->AddNamespace(S"bk", S"urn:samples");
   expr->SetContext(nsmgr);

   //Display the selection.
   XPathNodeIterator* iterator = nav->Select(expr);
   while (iterator->MoveNext()){
      Console::WriteLine(iterator->Current);
   }
}

[Visual Basic, C#, C++] サンプルでは、次の入力ファイルを使用します。

[Visual Basic, C#, C++] booksort.xml

<?xml version="1.0"?>
<!-- a fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
  <book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
    <title>Pride And Prejudice</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>24.95</price>
  </book>
  <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
    <title>The Handmaid's Tale</title>
    <author>
      <first-name>Margaret</first-name>
      <last-name>Atwood</last-name>
    </author>
    <price>29.95</price>
  </book>
  <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
    <title>Emma</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
  <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
    <title>Sense and Sensibility</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
</bookstore>

[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 ファミリ

参照

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