本文整理汇总了C#中System.Xml.XmlTextReader.XmlTextReader构造函数的典型用法代码示例。如果您正苦于以下问题:C# XmlTextReader构造函数的具体用法?C# XmlTextReader怎么用?C# XmlTextReader使用的例子?那么, 这里精选的构造函数代码示例或许可以为您提供帮助。您也可以进一步了解该构造函数所在类System.Xml.XmlTextReader
的用法示例。
在下文中一共展示了XmlTextReader构造函数的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
//Create the XML fragment to be parsed.
string xmlFrag ="<book> " +
"<title>Pride And Prejudice</title>" +
"<bk:genre>novel</bk:genre>" +
"</book>";
//Create the XmlNamespaceManager.
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace("bk", "urn:sample");
//Create the XmlParserContext.
XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);
//Create the reader.
XmlTextReader reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);
//Parse the XML. If they exist, display the prefix and
//namespace URI of each element.
while (reader.Read()){
if (reader.IsStartElement()){
if (reader.Prefix==String.Empty)
{
Console.WriteLine("<{0}>", reader.LocalName);
}
else
{
Console.Write("<{0}:{1}>", reader.Prefix, reader.LocalName);
Console.WriteLine(" The namespace URI is " + reader.NamespaceURI);
}
}
}
//Close the reader.
reader.Close();
}
} // End class
示例2: Main
//引入命名空间
using System;
using System.IO;
using System.Xml;
public class Sample {
private const String filename = "items.xml";
public static void Main() {
XmlTextReader reader = null;
try {
// Load the reader with the data file and ignore all white space nodes.
reader = new XmlTextReader(filename);
reader.WhitespaceHandling = WhitespaceHandling.None;
// Parse the file and display each of the nodes.
while (reader.Read()) {
switch (reader.NodeType) {
case XmlNodeType.Element:
Console.Write("<{0}>", reader.Name);
break;
case XmlNodeType.Text:
Console.Write(reader.Value);
break;
case XmlNodeType.CDATA:
Console.Write("<![CDATA[{0}]]>", reader.Value);
break;
case XmlNodeType.ProcessingInstruction:
Console.Write("<?{0} {1}?>", reader.Name, reader.Value);
break;
case XmlNodeType.Comment:
Console.Write("<!--{0}-->", reader.Value);
break;
case XmlNodeType.XmlDeclaration:
Console.Write("<?xml version='1.0'?>");
break;
case XmlNodeType.Document:
break;
case XmlNodeType.DocumentType:
Console.Write("<!DOCTYPE {0} [{1}]", reader.Name, reader.Value);
break;
case XmlNodeType.EntityReference:
Console.Write(reader.Name);
break;
case XmlNodeType.EndElement:
Console.Write("</{0}>", reader.Name);
break;
}
}
}
finally {
if (reader!=null)
reader.Close();
}
}
} // End class
示例3: Main
//引入命名空间
using System;
using System.IO;
using System.Xml;
public class Sample {
public static void Main() {
string xmlData =
@"<book>
<title>Oberon's Legacy</title>
<price>5.95</price>
</book>";
// Create the reader.
XmlTextReader reader = new XmlTextReader(new StringReader(xmlData));
reader.WhitespaceHandling = WhitespaceHandling.None;
// Display each element node.
while (reader.Read()){
switch (reader.NodeType){
case XmlNodeType.Element:
Console.Write("<{0}>", reader.Name);
break;
case XmlNodeType.Text:
Console.Write(reader.Value);
break;
case XmlNodeType.EndElement:
Console.Write("</{0}>", reader.Name);
break;
}
}
// Close the reader.
reader.Close();
}
} // End class