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 來加入預設命名空間。

注意XmlNamespaceManager 將用於解析 XML 路徑語言 (XPath) 運算式中的命名空間,則必須指定前置詞。 如果 XPath 運算式不包括前置詞,則會假設命名空間統一資源識別項 (URI) 為空命名空間。 如需有關 XPath 運算式以及 XmlNamespaceManager 的詳細資訊,請參考 SelectNodes(String)SetContext(XmlNamespaceManager) 方法。

uri
String

要加入的命名空間。

例外狀況

prefix 的值為 "xml" 或 "xmlns"。

prefixuri 的值為 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) Namespaces 規格有效 XML 名稱。 XmlNamespaceManager 會由 XmlReader 內部使用,因此為了避免重複工作, XmlNamespaceManager 假設所有前置詞和命名空間都有效。

如果前置詞和命名空間已存在於目前範圍內,新的前置詞和命名空間組將會取代現有的前置詞/命名空間組合。 不同的範圍可以存在相同的前置詞和命名空間組合。

預設會將下列前置詞/命名空間組新增至 XmlNamespaceManager 。 它們可以在任何範圍中判斷。

前置詞 命名空間
xmlns http://www.w3.org/2000/xmlns/ (xmlns 前置詞命名空間)
Xml http://www.w3.org/XML/1998/namespace (XML 命名空間)
String.Empty String.Empty (空命名空間) 。 此值可以重新指派不同的前置詞。 例如,xmlns=「」 會將預設命名空間定義為空的命名空間

適用於

另請參閱