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를 사용합니다.

참고 XPath(XML Path Language) 식에서 네임스페이스를 확인하는 데 XmlNamespaceManager를 사용할 경우에는 접두사를 지정해야 합니다. XPath 식에 접두사가 없으면 네임스페이스 URI(Uniform Resource Identifier)를 빈 네임스페이스로 간주합니다. XPath 식 및 XmlNamespaceManager에 대한 자세한 내용은 SelectNodes(String)SetContext(XmlNamespaceManager) 메서드를 참조하십시오.

uri
String

추가할 네임스페이스입니다.

예외

prefix의 값이 "xml" 또는 "xmlns"인 경우

prefix 또는 uri의 값이 null인 경우

예제

다음 예제에서는 XML 조각에서 네임스페이스를 확인하는 데 사용합니다 XmlNamespaceManager .

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은 규칙 및 uri 규격을 확인하지 prefix 않습니다.

XmlReader 는 접두사 및 네임스페이스를 포함한 이름을 검사하여 W3C(World Wide Web Consortium) 네임스페이스 사양에 따라 유효한 XML 이름이 되도록 합니다. XmlNamespaceManager 는 내부적으로 XmlReader사용되므로 작업이 XmlNamespaceManager 중복되지 않도록 모든 접두사 및 네임스페이스가 유효하다고 가정합니다.

접두사 및 네임스페이스가 현재 범위 내에 이미 있는 경우 새 접두사 및 네임스페이스 쌍이 기존 접두사/네임스페이스 조합을 대체합니다. 동일한 접두사 및 네임스페이스 조합이 서로 다른 범위에 존재할 수 있습니다.

다음 접두사/네임스페이스 쌍은 기본적으로 .에 XmlNamespaceManager추가됩니다. 모든 범위에서 확인할 수 있습니다.

접두사 네임스페이스
xmlns http://www.w3.org/2000/xmlns/ (xmlns 접두사 네임스페이스)
Xml http://www.w3.org/XML/1998/namespace (XML 네임스페이스)
String.Empty String.Empty(빈 네임스페이스). 이 값은 다른 접두사로 다시 할당할 수 있습니다. 예를 들어 xmlns=""는 기본 네임스페이스를 빈 네임스페이스로 정의합니다.

적용 대상

추가 정보