关于SelectNodes数据查找求解

cqhydz 2008-01-23 12:44:14
  
<note>

<from>Jani2</from>
<table>
<tr>
<td>
<table>
<tr>
<td><a href="www.yahoo1.com.cn">wwww1</a>
</td>
</tr>
</table>
<table>
<tr>
<td><a href="www.yahoo7.com.cn">XXXXX</a>
</td>
</tr>
</table>
<table id="t0">
<tr>
<td><a href="abc">wwww2</a>
</td>
</tr>
</table>
<table id="t1">
<tr>
<td><a href="javascritp::void()">wwww3</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
</note>


XmlNodeList subRoots = root.SelectNodes( "//table[@id] ");

问题是这样的,大家看到了一些有id属性的table了吧,我现在想取的就是那些table中的a> href里的数据,但由这些table是动态的比如还有t2 t3 t4.... 数量,现在要做两点一是知道table有多少个,还有就是依次取出需要的数据
谢谢大家

...全文
258 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cqhydz 2008-01-23
  • 打赏
  • 举报
回复
谢谢楼上的
这个我是知道的,有没有xpath方式的,另我现在已将所有<a>的找到了,但比如我只要href中含用go.asp的<a>而其它的不需要,这里如何处理呢,我现在用的是苯方法在代码中去对比,我想xpath有没有这模糊函数呢
nj_1st_excellence 2008-01-23
  • 打赏
  • 举报
回复
现在a里面的数据能取到了,如何取 a的href里的数据呢
==>
node.Attributes["href"]
cqhydz 2008-01-23
  • 打赏
  • 举报
回复
现在a里面的数据能取到了,如何取 a的href里的数据呢
ProjectDD 2008-01-23
  • 打赏
  • 举报
回复
UP
saucer 2008-01-23
  • 打赏
  • 举报
回复
XmlNodeList subRoots = root.SelectNodes( "//table[@id] ");

subRoots.Count应该告诉你有id属性的table有多少个

然后,要获取a节点的话

foreach(XmlElement t in subRoots)
{
XmlElement a = (XmlElement)t.SelectSingleNode(".//a");
Console.WriteLine(a.OuterXml);
}

或者也可以这样

XmlNodeList aNodes = root.SelectNodes( "//table[@id]//a");

foreach(XmlElement t in aNodes)
{
Console.WriteLine(t.OuterXml);
}
nj_1st_excellence 2008-01-23
  • 打赏
  • 举报
回复
可以模糊查询
SelectNodes("//a[contains(@href,'j')]")
这个是查询所有a的节点href中含有'j'的例子.

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧