如何查出指定元素节点中的指定属性值的那条节点

OICQshenshi 2010-04-02 10:54:14
bookstore.xml
<?xml version="1.0" encoding="utf-8"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<applenote genre="santase" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</applenote>
<book genre="fantasy" ISBN="2-3631-5">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
</bookstore>


OPXML.cs

        private void button3_Click(object sender, EventArgs e)
{

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFile);
XmlNode selnode = xmlDoc.SelectSingleNode("//book[@genre='fantasy']");
string txt = selnode.InnerText;
MessageBox.Show(txt);
}


我要找出<book>节点的属性名为genre且属性值等于fantasy的那个节点,请教xpath该如何写
...全文
64 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
OICQshenshi 2010-04-02
  • 打赏
  • 举报
回复
string nodeName;//节点名称
string nodeAttName;//节点属性名
string oldNodeAttValue;//属性值

xmlDoc.SelectSingleNode("//"+nodeName+"[@"+nodeAttName+"='"+oldNodeAttValue+"']");
oldeAttValue的变量写法正确不?

8,906

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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