XmlNamespaceManager.AddNamespace メソッド
アセンブリ: System.Xml (system.xml.dll 内)

Dim instance As XmlNamespaceManager Dim prefix As String Dim uri As String instance.AddNamespace(prefix, uri)
- prefix
追加する名前空間に関連付けるプリフィックス。String.Empty を使用して、既定の名前空間を追加します。
メモ
XML Path Language (XPath) 式の名前空間の解決に XmlNamespaceManager を使用する場合は、プレフィックスを指定する必要があります。XPath 式にプレフィックスが含まれていない場合、名前空間 URI (Uniform Resource Identifier) は、空の名前空間であると見なされます。XPath 式および XmlNamespaceManager の詳細については、XmlNode.SelectNodes メソッドおよび XPathExpression.SetContext メソッドの説明を参照してください。


XmlNamespaceManager は、prefix および uri の準拠性をチェックしません。
XmlReader は、プレフィックスおよび名前空間を含めて名前をチェックし、W3C (World Wide Web Consortium) の仕様に従う有効な XML 名であることを確認します。XmlNamespaceManager は XmlReader によって内部的に使用されるため、処理の重複を避けるために、XmlNamespaceManager はすべてのプレフィックスおよび名前空間を有効と見なします。
プレフィックスと名前空間が既に現在のスコープ内に存在している場合は、新しいプレフィックスと名前空間のペアが既存のプレフィックスと名前空間の組み合わせを置き換えます。異なるスコープ間で同じプリフィックスと名前空間の組み合わせが存在してもかまいません。
既定では、次のプリフィクスと名前空間のペアが XmlNamespaceManager に追加されます。これらの組み合わせはどのスコープでも確認できます。

XmlNamespaceManager を使用して、XML フラグメントの名前空間を解決する例を次に示します。
Imports System 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
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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からXmlNamespaceManager.AddNamespace メソッドを検索する場合は、下記のリンクをクリックしてください。

- XmlNamespaceManager.AddNamespace メソッドのページへのリンク