早上以前都正常,弄的时候SelectSingleNode还能找到数据
下午怎么就不行了,反复调试的时候观察到:
root正常。
bookroot不正常,只返回第一条。参数bname输入正常。
nodels正常。这里也用到了bname,都能找到数据。
public static void removebookmark(string bname,string mark)
{
string file = "d:\\book.xml";
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(file);
XmlNode root = xmldoc.SelectSingleNode("books");
XmlNode bookroot = xmldoc.SelectSingleNode("books/book['" + bname + "']");
if (mark == name)
{
bookroot.RemoveAll();
root.RemoveChild(bookroot);
}
else
{
XmlNodeList nodels = xmldoc.SelectNodes("books/book['" + bname + "']/mark");
foreach (XmlNode nd in nodels)
{
if (nd.InnerText == mark)
{
bookroot.RemoveChild(nd);
}
}
}
xmldoc.Save(file);
}
<?xml version="1.0" encoding="utf-8"?>
<books>
<book bname="687" url="C:\Documents and Settings\Administrator\桌面\687.txt">
<mark>1</mark>
<mark>3</mark>
</book>
<book bname="1321622" url="C:\Documents and Settings\Administrator\桌面\1321622.txt">
<mark>11</mark>
<mark>31</mark>
</book>
</books>
string file = "d:\\book.xml";
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(file);
XmlNode root = xmldoc.DocumentElement.SelectSingleNode("//books");//这句代码一般是为了获取根节点
OptionXml(rootNode);
//赋完值保存文档
xd.Save(filename);
/// <summary>
///
/// </summary>
/// <param name="root">根节点</param>
protected void OptionXml(XmlNode root)
{
foreach (XmlNode node in root.ChildNodes)
{
if (node.Name.Contains("file"))
{
node.InnerText = "hello world";
}
//递归遍历
OptionXml(node);
}
}
希望能帮到你
SelectSingleNode本來就是為了返回第一個XMLNode