XmlNamespaceManager.AddNamespace(String, String) Metoda

Definicja

Dodaje daną przestrzeń nazw do kolekcji.

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)

Parametry

prefix
String

Prefiks do skojarzenia z dodaną przestrzenią nazw. Użyj ciągu.Empty, aby dodać domyślną przestrzeń nazw.

Uwaga XmlNamespaceManager Jeśli zostanie użyty do rozpoznawania przestrzeni nazw w wyrażeniu XML Path Language (XPath), należy określić prefiks. Jeśli wyrażenie XPath nie zawiera prefiksu, zakłada się, że przestrzeń nazw Uniform Resource Identifier (URI) jest pustą przestrzenią nazw. Aby uzyskać więcej informacji na temat wyrażeń XPath i XmlNamespaceManagermetody , zapoznaj się z SelectNodes(String) metodami i SetContext(XmlNamespaceManager) .

uri
String

Przestrzeń nazw do dodania.

Wyjątki

Wartość parametru prefix to "xml" lub "xmlns".

Wartość lub prefix uri to null.

Przykłady

W poniższym przykładzie użyto XmlNamespaceManager metody rozpoznawania przestrzeni nazw w fragmentcie 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

Uwagi

XmlNamespaceManager nie sprawdza prefix zgodności i uri pod kątem zgodności.

XmlReader sprawdza nazwy, w tym prefiksy i przestrzenie nazw, aby upewnić się, że są prawidłowe nazwy XML zgodnie ze specyfikacją przestrzeni nazw World Wide Web Consortium (W3C). XmlNamespaceManager program jest używany wewnętrznie przez XmlReaderprogram , więc aby uniknąć duplikowania wysiłków, zakłada się, XmlNamespaceManager że wszystkie prefiksy i przestrzenie nazw są prawidłowe.

Jeśli prefiks i przestrzeń nazw już istnieją w bieżącym zakresie, nowa para prefiksu i przestrzeni nazw zastąpi istniejącą kombinację prefiksu/przestrzeni nazw. Ta sama kombinacja prefiksu i przestrzeni nazw może istnieć w różnych zakresach.

Następujące pary prefiksu/przestrzeni nazw są domyślnie dodawane do elementu XmlNamespaceManager. Można je określić w dowolnym zakresie.

Prefiks Przestrzeń nazw
Xmlns http://www.w3.org/2000/xmlns/ (przestrzeń nazw prefiksu xmlns)
xml http://www.w3.org/XML/1998/namespace (przestrzeń nazw XML)
String.empty String.Empty (pusta przestrzeń nazw). Tę wartość można ponownie przypisać innym prefiksem. Na przykład xmlns="" definiuje domyślną przestrzeń nazw jako pustą przestrzeń nazw

Dotyczy

Zobacz też