XmlNamespaceManager.AddNamespace(String, String) XmlNamespaceManager.AddNamespace(String, String) XmlNamespaceManager.AddNamespace(String, String) XmlNamespaceManager.AddNamespace(String, String) Method

定義

將指定的命名空間加入至集合中。Adds the given namespace to the collection.

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 String String

與要加入的命名空間關聯的前置詞。The prefix to associate with the namespace being added. 使用 String.Empty 來加入預設命名空間。Use String.Empty to add a default namespace.

附註:如果 XmlNamespaceManager 將用於解析 XML 路徑語言 (XPath) 運算式中的命名空間,則必須指定前置詞。NoteIf the XmlNamespaceManager will be used for resolving namespaces in an XML Path Language (XPath) expression, a prefix must be specified. 如果 XPath 運算式不包括前置詞,則會假設命名空間「統一資源識別元 (URI)」為空命名空間。If an XPath expression does not include a prefix, it is assumed that the namespace Uniform Resource Identifier (URI) is the empty namespace. 如需有關 XPath 運算式以及 XmlNamespaceManager 的詳細資訊,請參考 SelectNodes(String)SetContext(XmlNamespaceManager) 方法。For more information about XPath expressions and the XmlNamespaceManager, refer to the SelectNodes(String) and SetContext(XmlNamespaceManager) methods.

uri
String String String String

要加入的命名空間。The namespace to add.

例外狀況

prefix 的值為 "xml" 或 "xmlns"。The value for prefix is "xml" or "xmlns".

prefixuri 的值為 nullThe value for prefix or uri is null.

範例

下列範例會使用XmlNamespaceManager來解析 XML 片段中的命名空間。The following example uses XmlNamespaceManager to resolve namespaces in an XML fragment.

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是否符合一致性。XmlNamespaceManager does not check prefix and uri for conformance.

XmlReader檢查名稱,包括前置詞和命名空間,以根據全球資訊網協會(W3C)命名空間規格,確保它們是有效的 XML 名稱。XmlReader checks names, including prefixes and namespaces, to ensure they are valid XML names according to the World Wide Web Consortium (W3C) Namespaces specification. XmlNamespaceManager由在內部XmlReader使用,因此為了避免重複工作, XmlNamespaceManager假設所有前置詞和命名空間都是有效的。XmlNamespaceManager is used internally by XmlReader, so to avoid a duplication of efforts, XmlNamespaceManager assumes all prefixes and namespaces are valid.

如果前置詞和命名空間已經存在於目前的範圍內,新的前置詞和命名空間配對將會取代現有的前置詞/命名空間組合。If the prefix and namespace already exist within the current scope, the new prefix and namespace pair will replace the existing prefix/namespace combination. 相同的前置詞和命名空間組合可以存在於不同的範圍內。The same prefix and namespace combination can exist across different scopes.

預設會將下列前置詞/命名空間配對加入XmlNamespaceManagerThe following prefix/namespace pairs are added by default to the XmlNamespaceManager. 可以在任何範圍判斷。They can be determined at any scope.

前置詞Prefix 命名空間Namespace
xmlnsxmlns http://www.w3.org/2000/xmlns/(xmlns 前置詞命名空間)http://www.w3.org/2000/xmlns/ (the xmlns prefix namespace)
xmlxml http://www.w3.org/XML/1998/namespace(XML 命名空間)http://www.w3.org/XML/1998/namespace (the XML namespace)
String.EmptyString.Empty 字串。空的(空白的命名空間)。String.Empty (the empty namespace). 這個值可以重新指派為不同的前置詞。This value can be reassigned a different prefix. 例如,xmlns = "" 會將預設命名空間定義為空的命名空間For example, xmlns="" defines the default namespace to be the empty namespace

適用於

另請參閱