語言

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 表達式未包含前綴,則假設命名空間 Uniform Resource Identifier(URI)為空命名空間。 欲了解更多關於 XPath 表達式及 XmlNamespaceManager的資訊,請參閱 SelectNodes(String) and 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 不檢查 prefix 並 uri 確認符合性。

XmlReader 檢查名稱,包括前綴與命名空間,以確保其是否符合萬維網聯盟(W3C) 命名空間規範的有效 XML 名稱。 XmlNamespaceManager 內部使用 XmlReader,為避免重複作業, XmlNamespaceManager 假設所有前綴與命名空間皆有效。

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

以下前綴/命名空間對預設會加入到 XmlNamespaceManager。 它們可以在任何範圍內判定。

前綴 命名空間
xmlns http://www.w3.org/2000/xmlns/ (XMNS 前綴命名空間)
Xml http://www.w3.org/XML/1998/namespace (XML 命名空間)
String.Empty String.Empty(空命名空間)。 這個值可以重新分配到不同的前綴。 例如,xmlns=“” 定義了預設命名空間為空命名空間

適用於

另請參閱