XmlNamespaceManager.AddNamespace(String, String) メソッド

定義

指定した名前空間をコレクションに追加します。

public:
 virtual void AddNamespace(System::String ^ prefix, System::String ^ uri);
public virtual void AddNamespace(string prefix, string uri);
abstract member AddNamespace : string * string -> unit
override this.AddNamespace : string * string -> unit
Public Overridable Sub AddNamespace (prefix As String, uri As String)

パラメーター

prefix
String

追加する名前空間に関連付けるプレフィックス。 String.Empty を使用して、既定の名前空間を追加します。

メモを使用して XML パス言語 (XPath) 式の名前空間を解決する場合は、プレフィックスを指定する必要があります。 XPath 式にプレフィックスが含まれていない場合は、名前空間 Uniform Resource Identifier (URI) が空の名前空間であると見なされます。 XPath 式と XmlNamespaceManagerの詳細については、 SelectNodes(String) メソッドと SetContext(XmlNamespaceManager) メソッドを参照してください。

uri
String

追加する名前空間。

例外

prefixの値は "xml" または "xmlns" です。

prefixまたはuriの値がnull

次の例では、 XmlNamespaceManager を使用して XML フラグメント内の名前空間を解決します。

using System;
using System.Xml;

public class Sample
{

    public static void Main()
    {

        XmlTextReader reader = null;

        try
        {

            // Create the string containing the XML to read.
            String xmlFrag = "<book>" +
                           "<title>Pride And Prejudice</title>" +
                           "<author>" +
                           "<first-name>Jane</first-name>" +
                           "<last-name>Austen</last-name>" +
                           "</author>" +
                           "<curr:price>19.95</curr:price>" +
                           "<misc>&h;</misc>" +
                           "</book>";

            // Create an XmlNamespaceManager to resolve namespaces.
            NameTable nt = new NameTable();
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
            nsmgr.AddNamespace(String.Empty, "urn:samples"); //default namespace
            nsmgr.AddNamespace("curr", "urn:samples:dollar");

            // Create an XmlParserContext.  The XmlParserContext contains all the information
            // required to parse the XML fragment, including the entity information and the
            // XmlNamespaceManager to use for namespace resolution.
            XmlParserContext context;
            String subset = "<!ENTITY h 'hardcover'>";
            context = new XmlParserContext(nt, nsmgr, "book", null, null, subset, null, null, XmlSpace.None);

            // Create the reader.
            reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);

            // Parse the file and display the node values.
            while (reader.Read())
            {
                if (reader.HasValue)
                    Console.WriteLine("{0} [{1}] = {2}", reader.NodeType, reader.Name, reader.Value);
                else
                    Console.WriteLine("{0} [{1}]", reader.NodeType, reader.Name);
            }
        }
        finally
        {
            if (reader != null)
                reader.Close();
        }
    }
} // End class
Imports System.Xml

Public Class Sample

    Public Shared Sub Main()

        Dim reader As XmlTextReader = Nothing

        Try

            ' Create the string containing the XML to read.
            Dim xmlFrag As String
            xmlFrag = "<book>" & _
                           "<title>Pride And Prejudice</title>" & _
                           "<author>" & _
                           "<first-name>Jane</first-name>" & _
                           "<last-name>Austen</last-name>" & _
                           "</author>" & _
                           "<curr:price>19.95</curr:price>" & _
                           "<misc>&h;</misc>" & _
                           "</book>"

            ' Create an XmlNamespaceManager to resolve namespaces.
            Dim nt As NameTable = New NameTable()
            Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(nt)
            nsmgr.AddNamespace(String.Empty, "urn:samples") 'default namespace
            nsmgr.AddNamespace("curr", "urn:samples:dollar")

            ' Create an XmlParserContext.  The XmlParserContext contains all the information
            ' required to parse the XML fragment, including the entity information and the
            ' XmlNamespaceManager to use for namespace resolution.
            Dim context As XmlParserContext
            Dim subset As String = "<!ENTITY h 'hardcover'>"
            context = New XmlParserContext(nt, nsmgr, "book", Nothing, Nothing, subset, Nothing, Nothing, XmlSpace.None)

            ' Create the reader.
            reader = New XmlTextReader(xmlFrag, XmlNodeType.Element, context)

            ' Parse the file and display the node values.
            While (reader.Read())
                If (reader.HasValue) Then
                    Console.WriteLine("{0} [{1}] = {2}", reader.NodeType, reader.Name, reader.Value)
                Else
                    Console.WriteLine("{0} [{1}]", reader.NodeType, reader.Name)
                End If
            End While

        Finally
            If Not (reader Is Nothing) Then
                reader.Close()
            End If
        End Try
    End Sub
End Class

注釈

XmlNamespaceManager では、 prefixuri の準拠は確認されません。

XmlReader は、プレフィックスや名前空間を含む名前をチェックして、World Wide Web Consortium (W3C) 名前空間の仕様に従って有効な XML 名であることを確認します。 XmlNamespaceManagerXmlReaderによって内部的に使用されるため、作業の重複を避けるために、 XmlNamespaceManager は、すべてのプレフィックスと名前空間が有効であると想定します。

プレフィックスと名前空間が現在のスコープ内に既に存在する場合は、新しいプレフィックスと名前空間のペアによって既存のプレフィックスと名前空間の組み合わせが置き換えられます。 同じプレフィックスと名前空間の組み合わせは、異なるスコープにまたがって存在できます。

既定では、次のプレフィックスと名前空間のペアが XmlNamespaceManagerに追加されます。 これらは、任意のスコープで決定できます。

プレフィックス 名前空間
xmlns http://www.w3.org/2000/xmlns/ (xmlns プレフィックス名前空間)
XML http://www.w3.org/XML/1998/namespace (XML 名前空間)
String.empty String.Empty (空の名前空間)。 この値は、別のプレフィックスに再割り当てできます。 たとえば、xmlns="" は、既定の名前空間を空の名前空間として定義します

適用対象

こちらもご覧ください