monkey_boys
monkey_boys

Reputation: 7348

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?

Upvotes: 0

Views: 372

Answers (2)

Mr. Smith
Mr. Smith

Reputation: 5558

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();

Upvotes: 3

Marc Gravell
Marc Gravell

Reputation: 1063015

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;

Upvotes: 0

Related Questions