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 添加默认命名空间。
注意XmlNamespaceManager如果用于解析 XML 路径语言(XPath)表达式中的命名空间,则必须指定前缀。 如果 XPath 表达式不包含前缀,则假定命名空间统一资源标识符(URI)为空命名空间。 有关 XPath 表达式和 XPath XmlNamespaceManager的详细信息,请参阅 SelectNodes(String) 和 SetContext(XmlNamespaceManager) 方法。
- uri
- String
要添加的命名空间。
例外
值为 prefix “xml”或“xmlns”。
或为
示例
以下示例用于 XmlNamespaceManager 解析 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
注解
XmlNamespaceManager 不检查 prefix 并 uri 符合性。
XmlReader 根据万维网联合会(W3C) 命名空间规范检查名称(包括前缀和命名空间),以确保这些名称是有效的 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=“” 将默认命名空间定义为空命名空间 |