XmlNamespaceManager.AddNamespace(String, String) Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Adds the given namespace to the collection.
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
The prefix to associate with the namespace being added. Use String.Empty to add a default namespace.
Note If the XmlNamespaceManager will be used for resolving namespaces in an XML Path Language (XPath) expression, a prefix must be specified. If an XPath expression does not include a prefix, it is assumed that the namespace Uniform Resource Identifier (URI) is the empty namespace. For more information about XPath expressions and the XmlNamespaceManager, refer to the SelectNodes(String) and SetContext(XmlNamespaceManager) methods.
- uri
- String
The namespace to add.
The value for prefix
is "xml" or "xmlns".
The value for prefix
or uri
is null
.
The following example uses XmlNamespaceManager to resolve namespaces in an XML fragment.
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 does not check prefix
and uri
for conformance.
XmlReader checks names, including prefixes and namespaces, to ensure they are valid XML names according to the World Wide Web Consortium (W3C) Namespaces specification. XmlNamespaceManager is used internally by XmlReader, so to avoid a duplication of efforts, XmlNamespaceManager assumes all prefixes and namespaces are valid.
If the prefix and namespace already exist within the current scope, the new prefix and namespace pair will replace the existing prefix/namespace combination. The same prefix and namespace combination can exist across different scopes.
The following prefix/namespace pairs are added by default to the XmlNamespaceManager. They can be determined at any scope.
Prefix | Namespace |
---|---|
xmlns | http://www.w3.org/2000/xmlns/ (the xmlns prefix namespace) |
xml | http://www.w3.org/XML/1998/namespace (the XML namespace) |
String.Empty | String.Empty (the empty namespace). This value can be reassigned a different prefix. For example, xmlns="" defines the default namespace to be the empty namespace |
Termék | Verziók |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET-visszajelzés
A(z) .NET egy nyílt forráskód projekt. Visszajelzés adásához válasszon egy hivatkozást: