xml SelectSingleNode 查找节点的问题

jcyluck 2008-01-29 06:10:39
- <p>
- <p1>
<p3 />
<p4 />
</p1>
<p2 />
</p>

上面的xml文件,用xdoc.SelectSingleNode("p//p3")查询p节点下面是否存在子节点p3.
为什么查询出有该节点存在?
实际上p下面只存在p1和p2两个子节点,而p3是属于p1的子节点。
是不是说SelectSingleNode()方法查询的是指定路径的所有子节点及这些子节点下包含的孙子节点?
如果才能只查询到指定路径的子节点,而不包含这些子节点下面的孙子节点?
...全文
798 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
HelloWife 2011-03-28
  • 打赏
  • 举报
回复
如果p是根节点,改成如下:
xdoc.SelectSingleNode("//p//p3")
vwxyzh 2008-01-30
  • 打赏
  • 举报
回复
//代表中间可有任意个节点的情况,lz看一下XPath,就明白了
  • 打赏
  • 举报
回复
xpaht改为p/p3
jcyluck 2008-01-30
  • 打赏
  • 举报
回复
谢谢二楼
不过你的代码对我的问题没有任何帮助。
jcyluck 2008-01-30
  • 打赏
  • 举报
回复
xdoc的实例:
XmlDocument xdoc = new XmlDocument();
消瘦的锁骨浩 2008-01-29
  • 打赏
  • 举报
回复
SelectSingleNode()方法查询的是指定路径的所有子节点及这些子节点下包含的孙子节点
我这有一个xml操作例子..你看看..

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//读取XML
//已知属性名
//XmlDocument xmlDoc = new XmlDocument();
//xmlDoc.Load(Server.MapPath("test.xml"));
//XmlNode xn = xmlDoc.SelectSingleNode("bookstore");
//XmlNodeList xnl = xn.ChildNodes;
//foreach (XmlNode xn1 in xnl)
//{
// XmlElement xe = (XmlElement)xn1;
// Response.Write(xe.GetAttribute("genre")+"<BR>");
// Response.Write(xe.GetAttribute("ISBN") + "<BR>");
// XmlNodeList subxn = xe.ChildNodes;
// foreach (XmlNode xn2 in subxn)
// {
// Response.Write(xn2.InnerText+"<BR>");
// }
// Response.Write("<BR>");
//}
//未知属性名
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(Server.MapPath("test.xml"));
XmlNode xn = XmlDoc.SelectSingleNode("bookstore");
XmlNodeList xnl = xn.ChildNodes;
foreach (XmlNode xn1 in xnl)
{
XmlElement xe = (XmlElement)xn1;
XmlAttributeCollection xc = xe.Attributes;
for (int i = 0; i < xe.Attributes.Count; i++)
{
Response.Write(xc.Item(i).Name + ": ");
Response.Write(xc.Item(i).Value + "<BR>");
}
XmlNodeList xnl2 = xe.ChildNodes;
foreach (XmlNode xn2 in xnl2)
{
Response.Write(xn2.InnerText);
Response.Write("<BR>");
}
Response.Write("<BR>");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
//写入XML
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(Server.MapPath("test.xml"));
XmlNode xn = XmlDoc.SelectSingleNode("bookstore");
XmlElement subxml1 = XmlDoc.CreateElement("book");
subxml1.SetAttribute("genre", "leo");
subxml1.SetAttribute("ISBN", "123-456-789");
XmlElement subXmlxe1 = XmlDoc.CreateElement("title");
subXmlxe1.InnerText = "饮食学";
subxml1.AppendChild(subXmlxe1);
XmlElement subXmlxe2 = XmlDoc.CreateElement("author");
subXmlxe2.InnerText = "秦浩";
subxml1.AppendChild(subXmlxe2);
XmlElement subXmlxe3 = XmlDoc.CreateElement("price");
subXmlxe3.InnerText = "888888";
subxml1.AppendChild(subXmlxe3);
xn.AppendChild(subxml1);
XmlDoc.Save(Server.MapPath("test.xml"));
}
protected void Button3_Click(object sender, EventArgs e)
{
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(Server.MapPath("test.xml"));
XmlNodeList xnl = XmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点
foreach(XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
if(xe.GetAttribute("genre")=="leo")
{
xe.SetAttribute("genre","qinhao");
XmlNodeList xel = xe.ChildNodes;
foreach(XmlNode xn1 in xel)
{
XmlElement xe1=(XmlElement)xn1;
if(xe1.Name=="title")
{
xe1.InnerText = "C#";
}
}
}
}
XmlDoc.Save(Server.MapPath("test.xml"));
}
protected void Button4_Click(object sender, EventArgs e)
{
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(Server.MapPath("test.xml"));
XmlNodeList xnl1 = XmlDoc.SelectSingleNode("bookstore").ChildNodes;
foreach(XmlNode xn1 in xnl1 )
{
XmlElement xe = (XmlElement)xn1;
if (xe.GetAttribute("genre") == "qinhao")
{
//xe.RemoveAttribute("genre");
xe.RemoveAll();
}
}
XmlDoc.Save(Server.MapPath("test.xml"));
}
}

AptSnail 2008-01-29
  • 打赏
  • 举报
回复
xdoc是哪个类的实例?

111,096

社区成员

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

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

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