本文整理汇总了C#中System.Xml.XmlNamespaceManager.AddNamespace方法的典型用法代码示例。如果您正苦于以下问题:C# XmlNamespaceManager.AddNamespace方法的具体用法?C# XmlNamespaceManager.AddNamespace怎么用?C# XmlNamespaceManager.AddNamespace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。
在下文中一共展示了XmlNamespaceManager.AddNamespace方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
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