Xml C#删除帮助?

时间:2009-08-28 06:59:21

标签: c# xml

<?xml version="1.0" encoding="utf-8" ?>

<NickContents>
    <Nick id="test" password="test1" />
    <Nick id="test2" password="test1" />
    <Nick id="nKm4T5c1UQKyfyVPscL99w==" password="nKm4T5c1UQKyfyVPscL99w==" />
    <Nick id="zrtcPuJwJLYtQYzyLqYXYA==" password="i+n+EXfFKHAMsCafvn1uiQ==" />
    <Nick id="Utn83sH6g1/8IO7GeE9NSA==" password="pnloAHE/nagl2kw23L+BsA==" />
</NickContents>

如何删除id = test?

的位置

2 个答案:

答案 0 :(得分:3)

你可以试试这个:

XmlDocument d = new XmlDocument();
d.Load("MyFileName.Xml");

XmlNode t = d.SelectSingleNode("/path/to/node[@id='test']");
t.ParentNode.RemoveChild(t);

d.Save();

答案 1 :(得分:0)

XmlDocument为例,将id视为属性:

XmlDocument doc = new XmlDocument();
doc.LoadXml(@"<xml>
  <row id='123'/>
  <row id='456'/>
  <row id='789'/>
</xml>");
XmlNode node = doc.SelectSingleNode("//row[@id=456]");
node.ParentNode.RemoveChild(node);
string s = doc.OuterXml;