您的位置:首页 > 其它

XML 读取器和编写器----从URL读取XML

2006-12-06 19:03 246 查看

如何从 URL 读取 XML

此示例阐释如何使用 XmlTextReader 类从 URL 读取 XML。
注意:此示例是如何从文件读取 XML主题的继续。




VB XmlReadFromUrl.aspx
[运行示例] | [查看源代码]
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。

function doClick(index, numTabs, id) {
document.all("tab" + id, index).className = "tab";
for (var i=1; i

td.code {
padding:0,10,0,10;
border-style:solid;
border-width:1;
border-bottom:0;
border-top:0;
border-right:0;
border-color:cccccc;
background-color:ffffee
}
td.tab {
text-align:center;
font:8pt verdana;
width:15%;
padding:3,3,3,3;
border-style:solid;
border-width:1;
border-right:0;
border-color:black;
background-color:eeeeee;
cursor:hand
}
td.backtab {
text-align:center;
font: 8pt verdana;
width:15%;
padding:3,3,3,3;
border-style:solid;
border-width:1;
border-right:0;
border-color:black;
background-color:cccccc;
cursor:hand
}
td.space {
width:70%;
font: 8pt verdana;
padding:0,0,0,0;
border-style:solid;
border-bottom:0;
border-right:0;
border-width:1;
border-color:cccccc;
border-left-color:black;
background-color:white
}

String URLString = "http://localhost/quickstart/howto/samples/Xml/XmlReadFromUrl/vb/books.xml";
// Load the XmlTextReader from the URL
myXmlURLreader = new XmlTextReader (URLString);

private const URLString as String = "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;
}
}

While reader.Read()
Select (reader.NodeType)
case XmlNodeType.XmlDeclaration:
Format (reader, "XmlDeclaration")
declarationCount += 1
case XmlNodeType.ProcessingInstruction:
Format (reader, "ProcessingInstruction")
piCount += 1
case XmlNodeType.DocumentType:
Format (reader, "DocumentType")
docCount += 1
case XmlNodeType.Comment:
Format (reader, "Comment")
commentCount += 1
case XmlNodeType.Element:
Format (reader, "Element")
elementCount += 1
if (reader.HasAttributes)
attributeCount += reader.AttributeCount
end if
case XmlNodeType.Text:
Format (reader, "Text")
textCount += 1
case XmlNodeType.Whitespace:
whitespaceCount += 1
End Select
End While

C# VB

摘要

XmlTextReader 提供一些构造函数,以从表示 URL 的字符串或文件名、流或 TextReader 读取 XML。
可使用 MoveToNextAttribute 方法访问属性节点,该方法使您可以确定属性节点的属性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: