XmlNamespaceManager.AddNamespace(String, String) 方法

定义

将给定的命名空间添加到集合。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

与要添加的命名空间关联的前缀。The prefix to associate with the namespace being added. 使用 String.Empty 来添加默认命名空间。Use String.Empty to add a default namespace.

注意:如果 XmlNamespaceManager 将用于解析 XML 路径语言 (XPath) 表达式中的命名空间,则必须指定前缀。Note If 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

要添加的命名空间。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 不检查 prefix uri 一致性。XmlNamespaceManager does not check prefix and uri for conformance.

XmlReader 检查名称(包括前缀和命名空间),以确保它们是有效的 XML 名称,具体取决于万维网联合会 (W3C) 命名空间规范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

适用于

另请参阅