首页 新闻 搜索 专区 学院

还是xmlreader的问题

0
[已解决问题] 解决于 2008-08-07 20:46
这个类实在是不熟悉,里面有很多问题解决不了。<div>XML文档如下:</div><div><div class="e"> <div class="c" style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><a class="b" onfocus="h()" onclick="return false" href="file:///K:/Project/NET/SEC/SEC/bin/Debug/Data/Favorites.xml#">-</a> <span class="m">&lt;</span><span class="t">Favorites</span><span class="m">></span></div> <div> <div class="e"> <div class="c" style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><a class="b" onfocus="h()" onclick="return false" href="file:///K:/Project/NET/SEC/SEC/bin/Debug/Data/Favorites.xml#">-</a> <span class="m">&lt;</span><span class="t">Folder</span><span class="t"> Name</span><span class="m">="</span><b>搜索</b><span class="m">"</span><span class="m">></span></div> <div> <div class="e"> <div class="c" style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><a class="b" onfocus="h()" onclick="return false" href="file:///K:/Project/NET/SEC/SEC/bin/Debug/Data/Favorites.xml#">-</a> <span class="m">&lt;</span><span class="t">Link</span><span class="m">></span></div> <div> <div class="e"> <div style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><span class="b"> </span> <span class="m">&lt;</span><span class="t">Title</span><span class="m">></span><span class="tx">百度</span><span class="m">&lt;/</span><span class="t">Title</span><span class="m">></span> </div></div> <div class="e"> <div style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><span class="b"> </span> <span class="m">&lt;</span><span class="t">Url</span><span class="m">></span><span class="tx">http://www.baidu.com</span><span class="m">&lt;/</span><span class="t">Url</span><span class="m">></span> </div></div> <div><span class="b"> </span> <span class="m">&lt;/</span><span class="t">Link</span><span class="m">></span></div></div></div> <div class="e"> <div class="c" style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><a class="b" onfocus="h()" onclick="return false" href="file:///K:/Project/NET/SEC/SEC/bin/Debug/Data/Favorites.xml#">-</a> <span class="m">&lt;</span><span class="t">Link</span><span class="m">></span></div> <div> <div class="e"> <div style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><span class="b"> </span> <span class="m">&lt;</span><span class="t">Title</span><span class="m">></span><span class="tx">搜狐</span><span class="m">&lt;/</span><span class="t">Title</span><span class="m">></span> </div></div> <div class="e"> <div style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><span class="b"> </span> <span class="m">&lt;</span><span class="t">Url</span><span class="m">></span><span class="tx">http://www.sohu.com</span><span class="m">&lt;/</span><span class="t">Url</span><span class="m">></span> </div></div> <div><span class="b"> </span> <span class="m">&lt;/</span><span class="t">Link</span><span class="m">></span></div></div></div> <div><span class="b"> </span> <span class="m">&lt;/</span><span class="t">Folder</span><span class="m">></span></div></div></div> <div class="e"> <div class="c" style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><a class="b" onfocus="h()" onclick="return false" href="file:///K:/Project/NET/SEC/SEC/bin/Debug/Data/Favorites.xml#">-</a> <span class="m">&lt;</span><span class="t">Folder</span><span class="t"> Name</span><span class="m">="</span><b>娱乐</b><span class="m">"</span><span class="m">></span></div> <div> <div class="e"> <div class="c" style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><a class="b" onfocus="h()" onclick="return false" href="file:///K:/Project/NET/SEC/SEC/bin/Debug/Data/Favorites.xml#">-</a> <span class="m">&lt;</span><span class="t">Link</span><span class="m">></span></div> <div> <div class="e"> <div style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><span class="b"> </span> <span class="m">&lt;</span><span class="t">Title</span><span class="m">></span><span class="tx">一听</span><span class="m">&lt;/</span><span class="t">Title</span><span class="m">></span> </div></div> <div class="e"> <div style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><span class="b"> </span> <span class="m">&lt;</span><span class="t">Url</span><span class="m">></span><span class="tx">http://www.1ting.com</span><span class="m">&lt;/</span><span class="t">Url</span><span class="m">></span> </div></div> <div><span class="b"> </span> <span class="m">&lt;/</span><span class="t">Link</span><span class="m">></span></div></div></div> <div class="e"> <div class="c" style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><a class="b" onfocus="h()" onclick="return false" href="file:///K:/Project/NET/SEC/SEC/bin/Debug/Data/Favorites.xml#">-</a> <span class="m">&lt;</span><span class="t">Link</span><span class="m">></span></div> <div> <div class="e"> <div style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><span class="b"> </span> <span class="m">&lt;</span><span class="t">Title</span><span class="m">></span><span class="tx">腾讯</span><span class="m">&lt;/</span><span class="t">Title</span><span class="m">></span> </div></div> <div class="e"> <div style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><span class="b"> </span> <span class="m">&lt;</span><span class="t">Url</span><span class="m">></span><span class="tx">http://www.qq.com</span><span class="m">&lt;/</span><span class="t">Url</span><span class="m">></span> </div></div> <div><span class="b"> </span> <span class="m">&lt;/</span><span class="t">Link</span><span class="m">></span></div></div></div> <div><span class="b"> </span> <span class="m">&lt;/</span><span class="t">Folder</span><span class="m">></span></div></div></div> <div><span class="b"> </span> <span class="m">&lt;/</span><span class="t">Favorites</span><span class="m">></span></div><div>我要得到所有Title和Url,但前面还要一个条件,就是通过参数Folder的属性name来进行查询,如果参数值是“搜索”,则得到的应该是“搜索”下的所有Title和Url。</div></div></div></div>
问题补充: 这个文档现在是不大,但我现在是想要熟悉这个类,并不是为了达到目的。谢谢。
少之的主页 少之 | 菜鸟二级 | 园豆:205
提问于:2008-08-07 16:18
< >
分享
最佳答案
0
使用XMLReader是一件非常复杂的事情。你需要确保任何时候你都能够清晰的了解当前位置在哪里 和 接下来会出现什么。 做搜索更是一件非常困难的事情。非常不建议你使用XmlReader做这个题。其实XmlDocument性能非常好,我曾经尝试加载过20+M的XML文件,也就延迟1-2秒。比记事本快多了。 如果你非要试一下,下面的代码也许能用。反正我是没打算调试和排错了。太累人了。 private void Form1_Load(object sender, EventArgs e) { XmlReaderSettings settings = new XmlReaderSettings(); settings.ConformanceLevel = ConformanceLevel.Fragment; settings.IgnoreWhitespace = true; settings.IgnoreComments = true; this.reader = XmlReader.Create(file, settings); this.reader.ReadToFollowing("Folder"); // Move to first folder string folders = ""; while (this.reader.IsStartElement("Folder")) { if (!IsMatch("搜索")) { this.reader.Skip(); // Skip all contents of current Folder element. continue; } if (reader.IsEmptyElement) { this.reader.Skip(); continue; } this.reader.Read(); // Skip start element of Folder. while (this.reader.IsStartElement("Link")) { reader.Read(); // Skip start element of Link. string title = reader.ReadContentAsString(); reader.ReadToNextSibling("Url"); // Move to Url element. string url = reader.ReadContentAsString(); reader.ReadEndElement(); // Read end element of Url. reader.ReadEndElement(); // Read end element of Link. Keep your mind nimble ;-) } reader.ReadEndElement(); // Read end element of Folder. } } private bool IsMatch(string name) { if (this.reader.HasAttributes) {
Colin Han | 老鸟四级 |园豆:3041 | 2008-08-07 18:39
其他回答(1)
0
如果xml文档不是很大,你可以用XmlDocument用XPath查询。
玉开 | 园豆:8822 (大侠五级) | 2008-08-07 16:46
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册