XmlNamespaceManager.AddNamespace(String, String) Metodo

Definizione

Aggiunge lo spazio dei nomi specificato alla raccolta.

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)

Parametri

prefix
String

Prefisso da associare allo spazio dei nomi da aggiungere. Usare String.Empty per aggiungere uno spazio dei nomi predefinito.

NotaXmlNamespaceManager Se verrà utilizzato per la risoluzione degli spazi dei nomi in un'espressione XPath (XML Path Language), è necessario specificare un prefisso. Se un'espressione XPath non include un prefisso, si presuppone che lo spazio dei nomi Uniform Resource Identifier (URI) sia lo spazio dei nomi vuoto. Per altre informazioni sulle espressioni XPath e su XmlNamespaceManager, vedere i SelectNodes(String) metodi e SetContext(XmlNamespaceManager) .

uri
String

Spazio dei nomi da aggiungere.

Eccezioni

Il valore per prefix è "xml" o "xmlns".

Il valore per prefix o uri è null.

Esempio

Nell'esempio seguente viene XmlNamespaceManager usato per risolvere gli spazi dei nomi in un frammento 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

Commenti

XmlNamespaceManager non verifica prefix e uri verifica la conformità.

XmlReader controlla i nomi, inclusi i prefissi e gli spazi dei nomi, per assicurarsi che siano nomi XML validi in base alla specifica W3C (World Wide Web Consortium). XmlNamespaceManager viene usato internamente da XmlReader, in modo da evitare una duplicazione delle attività, XmlNamespaceManager presuppone che tutti i prefissi e gli spazi dei nomi siano validi.

Se il prefisso e lo spazio dei nomi esistono già nell'ambito corrente, la nuova coppia di prefissi e spazi dei nomi sostituirà la combinazione di prefisso/spazio dei nomi esistente. La stessa combinazione di prefisso e spazio dei nomi può esistere in ambiti diversi.

Le coppie di prefisso/spazio dei nomi seguenti vengono aggiunte per impostazione predefinita a XmlNamespaceManager. Possono essere determinati in qualsiasi ambito.

Prefisso Namespace
xmlns http://www.w3.org/2000/xmlns/ (spazio dei nomi del prefisso xmlns)
xml http://www.w3.org/XML/1998/namespace (spazio dei nomi XML)
Empty String.Empty (spazio dei nomi vuoto). Questo valore può essere riassegnato con un prefisso diverso. Ad esempio, xmlns="" definisce lo spazio dei nomi predefinito come spazio dei nomi vuoto

Si applica a

Vedi anche