从 XmlDocument 到 XmlReader .Net
在回答了我的问题的用户的建议后,我尝试将我的 XmlDocument 代码转换为 XmlReader 代码,但我遇到了一些麻烦。
这是 XML(从 php-mysql 页面生成)
<row>
<idLink>64</idLink>
<idHost>3</idHost>
<url>http://www.google.com</url>
</row>
<row>
<idLink>68</idLink>
<idHost>4</idHost>
<url>http://www.bing.com</url>
</row>
..... until about 10000 rows
这是我的 XmlDocument 代码:
xmlDoc.Load("http://www.myUrl.com/list.php");
if (xmlDoc.DocumentElement != null){
foreach (XmlNode node in xmlDoc.DocumentElement)
{
if (node.Name == "row")
{
list.Add(new Links {
idLink = Convert.ToInt32(node.ChildNodes[0].InnerText),
idHost = Convert.ToInt32(node.ChildNodes[1].InnerText),
url = node.ChildNodes[2].InnerText });
}
}
return list;
现在我在 XmlReader 中转换时遇到一些麻烦,我尝试了很多代码,但无法处理它。
using (XmlReader reader = new XmlTextReader("http://myUrl.com/list.php"))
{
if (reader.NodeType == XmlNodeType.Element)
?????
After an advice from a user that answered to my question I'm tring to convert my XmlDocument code to XmlReader code but I'm having some troubles.
This is XML (generated from php-mysql Page)
<row>
<idLink>64</idLink>
<idHost>3</idHost>
<url>http://www.google.com</url>
</row>
<row>
<idLink>68</idLink>
<idHost>4</idHost>
<url>http://www.bing.com</url>
</row>
..... until about 10000 rows
This is my XmlDocument code:
xmlDoc.Load("http://www.myUrl.com/list.php");
if (xmlDoc.DocumentElement != null){
foreach (XmlNode node in xmlDoc.DocumentElement)
{
if (node.Name == "row")
{
list.Add(new Links {
idLink = Convert.ToInt32(node.ChildNodes[0].InnerText),
idHost = Convert.ToInt32(node.ChildNodes[1].InnerText),
url = node.ChildNodes[2].InnerText });
}
}
return list;
Now I have some trouble to Convert in XmlReader, I tried many code but I can't handle it.
using (XmlReader reader = new XmlTextReader("http://myUrl.com/list.php"))
{
if (reader.NodeType == XmlNodeType.Element)
?????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
参见:http://blog.jongallant.com/ 2007/01/convert-xmldocument-to-xmlreader.html
You can use
See: http://blog.jongallant.com/2007/01/convert-xmldocument-to-xmlreader.html
如果您对 xml 文件执行只读操作,那么您可以使用 XmlReader,但正如 @Marc Gravell 指出的那样,这很困难。
在这种情况下,我将创建一个使用
XmlReader
包装XPathDocument
的类。然后,我创建一个XPathNavigator
来读取数据。下面是一个例子:如上所示,然后可以将数据的读取封装到关联的对象模型中。您可以在我对此问题的回答中看到一些详细信息:
如何一次操作一个父元素的 XML 文档?
If you are doing read only operations on an xml file then you can you use
XmlReader
but as @Marc Gravell points out it is difficult.In this situation I will create a class that wraps an
XPathDocument
using anXmlReader
. I then create anXPathNavigator
to read the data. Here's an example:As shown above, the reading of the data can then be encapsulated into an associated object model. You can see some details in my answer on this question:
How do I manipulate an XML document one parent element at a time?