XmlNamespaceManager.AddNamespace(String, String) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定した名前空間をコレクションに追加します。
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 を使用して、既定の名前空間を追加します。
注 XML Path Language (XPath) 式の名前空間の解決に 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 は、プレフィックスや名前空間を含む名前をチェックして、World Wide Web Consortium (W3C) 名前空間の仕様に従って有効な XML 名であることを確認します。 XmlNamespaceManager は内部的 XmlReaderに使用されるため、作業の重複を避けるために、 XmlNamespaceManager すべてのプレフィックスと名前空間が有効であると見なされます。
プレフィックスと名前空間が現在のスコープ内に既に存在する場合は、新しいプレフィックスと名前空間のペアによって既存のプレフィックスと名前空間の組み合わせが置き換えられます。 同じプレフィックスと名前空間の組み合わせが異なるスコープに存在する可能性があります。
既定では XmlNamespaceManager、次のプレフィックスと名前空間のペアが . これらは、任意のスコープで決定できます。
| Prefix | 名前空間 |
|---|---|
| xmlns | http://www.w3.org/2000/xmlns/ (xmlns プレフィックス名前空間) |
| xml | http://www.w3.org/XML/1998/namespace (XML 名前空間) |
| String.Empty | String.Empty (空の名前空間)。 この値は、別のプレフィックスに再割り当てできます。 たとえば、xmlns="" は、既定の名前空間を空の名前空間として定義します |