C# xml SelectSingleNode 问题

tyzyx 2009-07-08 05:04:13

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(@"<students>
<student pkid='111'>
<name>aaa</name>
<age>22</age>
</student>
<student pkid='222'>
<name>bbb</name>
<age>24</age>
</student>
</students>"
);
XmlNode node = xmlDoc.SelectSingleNode("students/student[@pkid='222']");
if (node != null)
{
Console.WriteLine(node.FirstChild.InnerText); //return bbb;
}

以上代码可以正常查找到student pkid 为 222的结点
但是如果XML文件格式如下:注意 <student pkid='222'>变为 <student pkid="222">,如何使用SelectSingleNode定位?

" <students>
<student pkid="111">
<name>aaa</name>
<age>22</age>
</student>
<student pkid="222">
<name>bbb</name>
<age>24</age>
</student>
</students>"
...全文
544 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
robin521 2009-07-09
  • 打赏
  • 举报
回复
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 XML示例 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{


}
}
public class XmlProvider : IDisposable
{
private string a;
private XmlDocument b;
public XmlProvider(string XmlFile)
{
try
{
this.b = new XmlDocument();
this.b.Load(XmlFile);
}
catch
{

}
this.a = XmlFile;

}
public void DeleteNode(string NodeName)
{
string xpath = NodeName.Substring(0,NodeName.LastIndexOf("/"));
this.b.SelectSingleNode(xpath).RemoveChild(this.b.SelectSingleNode(NodeName));
}
public void Dispose()
{
this.Dispose(true);
}
protected void Dispose(bool Diposing)
{
this.b = null;

}
public void InsetElement(string mainNode, string element, string content)
{

XmlNode node = this.b.SelectSingleNode(mainNode);
XmlElement newChild = this.b.CreateElement(element);
newChild.InnerText = content;
node.AppendChild(newChild);


}
public void InsertElement(string mainNode, string element, string attrib, string attribConte, string content)
{
XmlNode node = this.b.SelectSingleNode(mainNode);
XmlElement newChild = this.b.CreateElement(element);
newChild.SetAttribute(attrib, attribConte);
newChild.InnerText = content;
node.AppendChild(newChild);

}
public void InsertNode(string mainNode, string childNode, string element, string contents)
{

XmlNode node = this.b.SelectSingleNode(mainNode);
XmlElement newChild = this.b.CreateElement(childNode);
node.AppendChild(newChild);
XmlElement element3 = this.b.CreateElement(element);
element3.InnerText = contents;
newChild.AppendChild(element3);

}
public string ReadAtribute(string PathNode, string AttubuteName)
{
string str = "";
try
{
str = this.b.SelectSingleNode(PathNode).Attributes[AttubuteName].Value;
}
catch
{

}
return str;
}
public string ReadInnerText(string PathNode)
{
return this.b.SelectSingleNode(PathNode).InnerText;
}
public XmlNode ReadNode(string PathNode)
{
return this.b.SelectSingleNode(PathNode);
}
public void Save()
{
try
{
this.b.Save(this.a);
}
catch
{

}
this.b = null;
}
public void UpdateAttribute(string PathNode, string AttributeName, int AttributeValue)
{
this.b.SelectSingleNode(PathNode).Attributes[AttributeName].Value = AttributeValue;
}
public void UpdateInnerText(string PathNode, string InnerText)
{
this.b.SelectSingleNode(PathNode).InnerText = InnerText;
}

}
whowhen21 2009-07-09
  • 打赏
  • 举报
回复
楼上有解了,用个转义字符来区分啊
LQknife 2009-07-09
  • 打赏
  • 举报
回复
用XmlNode node = xmlDoc.SelectSingleNode("students/student[@pkid='222']");
这个在检验一次就知道是不是一样了
g394594141 2009-07-08
  • 打赏
  • 举报
回复
估计2楼的可以
Jock.Chen 2009-07-08
  • 打赏
  • 举报
回复
同意2楼.
sunshine_anycall 2009-07-08
  • 打赏
  • 举报
回复
xpath参数变一下就可以
  • 打赏
  • 举报
回复
<student pkid='222'>变为 <student pkid="222">

xpath的定义都是一样的。

他们的区别是字符串里的"要转义一下。 \"
  • 打赏
  • 举报
回复
应该一样的吧。。。
zgke 2009-07-08
  • 打赏
  • 举报
回复
XmlNode node = xmlDoc.SelectSingleNode("students/student[@pkid=\"222\"]");
  • 打赏
  • 举报
回复
有区别吗?

110,529

社区成员

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

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

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