简体   繁体   中英

Xml C# Delete Help?

<?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>

how to delete where id = test?

You could try this:

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

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

d.Save();

Using XmlDocument as an example, and treating id as an attribute:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM