XML 读取器和编写器----从URL读取XML
如何从 URL 读取 XML
此示例阐释如何使用 XmlTextReader 类从 URL 读取 XML。
注意:此示例是如何从文件读取 XML主题的继续。
[运行示例] | [查看源代码] |
XmlTextReader 有不同的构造函数,以指定 XML 数据的位置。此示例从以下特定语言的 URL 之一加载 XmlTextReader:http://localhost/quickstart/howto/samples/Xml/XmlReadFromUrl/vb/books.xml 或 http://localhost/quickstart/howto/samples/Xml/XmlReadFromUrl/cs/books.xml。下列示例代码构造一个 XmlTextReader。
String URLString = "http://localhost/quickstart/howto/samples/Xml/XmlReadFromUrl/vb/books.xml";
// Load the XmlTextReader from the URL
myXmlURLreader = new XmlTextReader (URLString);
|
||
| C# | VB | |
加载完成后,该示例代码调用 FormatXML 函数。在此函数中,XmlTextReader 使用 Read 方法在 XML 数据中移动,按顺序读取数据,以获取下一个节点。如果再没有节点,此函数则返回假。有关 Read 方法的操作原理的更多信息,请参阅如何从文件读取 XML
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.XmlDeclaration:
Format (reader, "XmlDeclaration");
declarationCount++;
break;
case XmlNodeType.ProcessingInstruction:
Format (reader, "ProcessingInstruction");
piCount++;
break;
case XmlNodeType.DocumentType:
Format (reader, "DocumentType");
docCount++;
break;
case XmlNodeType.Comment:
Format (reader, "Comment");
commentCount++;
break;
case XmlNodeType.Element:
Format (reader, "Element");
elementCount++;
if (reader.HasAttributes)
attributeCount += reader.AttributeCount;
break;
case XmlNodeType.Text:
Format (reader, "Text");
textCount++;
break;
case XmlNodeType.Whitespace:
whitespaceCount++;
break;
}
}
|
||
| C# | VB | |
摘要
- XmlTextReader 提供一些构造函数,以从表示 URL 的字符串或文件名、流或 TextReader 读取 XML。
- 可使用 MoveToNextAttribute 方法访问属性节点,该方法使您可以确定属性节点的属性。
浙公网安备 33010602011771号