25

Possible Duplicate:
How to remove an XmlNode from XmlNodeList

Hi, How can i delete a set of nodes from an XML file.? Here is a code snippet.

string path = @"C:\Documents and Settings\e454935\Desktop\NUnitSettings.xml";
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
xmldoc.Load(fs);
fs.Close();
xmldoc.DocumentElement.RemoveChild(xmldoc.DocumentElement.ChildNodes[1]);
FileStream WRITER = new FileStream(path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
xmldoc.Save(WRITER);
WRITER.Close(); 

I tried the following code simply to delete a node and got "Object reference not set to an instance of an object." at

xmldoc.DocumentElement.RemoveChild(xmldoc.DocumentElement.ChildNodes[1]);

Here is a sample XML file,

<?xml version="1.0"?>
<Xml1>
  <Settings>
    <Setting name="DisplayFormat" value="Full" />
    <Setting name="File1" value="a" />
    <Setting name="File1" value="b" />
    <Setting name="File1" value="c" />
    <Setting name="File1" value="d" />
  </Settings>
</Xml1>

Actually from this file i want to delete the Four File1 nodes which has the values "a,b,c,d" and then i want to add a node,

<Setting name="File1" value="e" />

How can i do this.?

Community
  • 1
  • 1
SyncMaster
  • 9,754
  • 34
  • 94
  • 137
  • 3
    This has been answered here http://stackoverflow.com/questions/875136 already, and probably two dozen times in other questions. Voted to close as a dupe. – Tomalak May 28 '09 at 07:48

4 Answers4

31

You can use Linq to XML to do this:

XDocument doc = XDocument.Load("input.xml");
var q = from node in doc.Descendants("Setting")
        let attr = node.Attribute("name")
        where attr != null && attr.Value == "File1"
        select node;
q.ToList().ForEach(x => x.Remove());
doc.Save("output.xml");
idursun
  • 6,261
  • 1
  • 37
  • 51
14

Deleting nodes from XML

            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            XmlNodeList nodes = doc.SelectNodes("//Setting[@name='File1']");
            for (int i = nodes.Count - 1; i >= 0; i--)
            {
                nodes[i].ParentNode.RemoveChild(nodes[i]);
            }
            doc.Save(path);

Adding attribute to Nodes in XML

    XmlDocument originalXml = new XmlDocument();
    originalXml.Load(path);
    XmlNode menu = originalXml.SelectSingleNode("//Settings");
    XmlNode newSub = originalXml.CreateNode(XmlNodeType.Element, "Setting", null);
    XmlAttribute xa = originalXml.CreateAttribute("name");
    xa.Value = "qwerty";
    XmlAttribute xb = originalXml.CreateAttribute("value");
    xb.Value = "555";
    newSub.Attributes.Append(xa);
    newSub.Attributes.Append(xb);
    menu.AppendChild(newSub);
    originalXml.Save(path);
SyncMaster
  • 9,754
  • 34
  • 94
  • 137
  • 2
    Not sure why you were downvoted, but +1 from me. That node.ParentNode.RemoveChild(node) is a handy line of code to remember. – Adam Plocher Feb 16 '14 at 13:03
8

It may be easier to use XPath to locate the nodes that you wish to delete. This stackoverflow thread might give you some ideas.

In your case you will find the four nodes that you want using this expression:

XmlDocument doc = new XmlDocument();
doc.Load(fileName);
XmlNodeList nodes = doc.SelectNodes("//Setting[@name='File1']");
Community
  • 1
  • 1
Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
3

DocumentElement is the root node of the document so childNodes[1] doesn't exist in that document. childNodes[0] would be the <Settings> node

Gonzalo.-
  • 12,512
  • 5
  • 50
  • 82
PQW
  • 1,147
  • 6
  • 6