Removing Nodes from an Xmldocument

Removing nodes from an XmlDocument

Instead of

configDoc.RemoveChild(projectNodes[i]);

try

projectNodes[i].parentNode.RemoveChild(projectNodes[i]);

Remove node from xmlDocument

The RemoveChild method working on direct child of the node. you are trying to remove some inner (not first generation) node by accessing the root node - the document.
The trick here is to get the parent node and remove the child from it.

child.ParentNode.RemoveChild(child)

Read an XML document and removing nodes

Using Xml Linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace XML
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);

XElement EmpDetail = doc.Descendants("EmpDetail").Where(x => (int)x.Element("RecordId") == 2).FirstOrDefault();

EmpDetail.Remove();

}
}
}

Removing a child node from XmlNode

You can get know the parent node:

node.ParentNode.RemoveChild(node);

Please note that the node.ParentNode can be null.

Remove an XML element based on child index

In your case looping XmlNodeList not required.

try this

XmlDocument doc = new XmlDocument();
doc.Load(path);

if (ListView1.SelectedIndex < doc.DocumentElement.ChildNodes.Count)
{
doc.DocumentElement.RemoveChild(doc.DocumentElement.ChildNodes[ListView1.SelectedIndex]);
doc.Save(path);
}

How to remove a XMLNode from XMLDocument occuring at multiple nested levels

As you indicated that you are working with an XmlDocument, you need to remove a child XmlElement node via the RemoveChild method on the parent node:

    string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<books>
<book>
<title>The Walking Dead</title>
<author>Test Name</author>
<isbn>1239859895</isbn>
</book>
<book>
<title>The Walking Dead</title>
<author>
<isbn>29893893893</isbn>
<firstname>test1</firstname>
<lastname>test</lastname>
</author>
</book>
</books>
";
// Initialize and load the XmlDocument
var doc = new XmlDocument();
doc.LoadXml(xml);

// Delete all XmlElements named "isbn".
var list = doc.DocumentElement.GetElementsByTagName("isbn").OfType<XmlElement>().ToArray();
foreach (var element in list)
{
var parent = element.ParentNode;
if (parent != null)
parent.RemoveChild(element);
}
var newXml = doc.OuterXml;
Debug.WriteLine(newXml);

And the output is:

<?xml version="1.0" encoding="utf-16"?>
<books>
<book>
<title>The Walking Dead</title>
<author>Test Name</author>
</book>
<book>
<title>The Walking Dead</title>
<author>
<firstname>test1</firstname>
<lastname>test</lastname>
</author>
</book>
</books>

Remove node from xmlDoc, how to delete the parentNode?

I think your issue is the XmlNode.RemoveAll() only removing the children of the node and the chkNode.ParentNode property is selecting the node with the <Set> tag. So when you remove all children, you are left with an empty <Set/> node. You need to be able to remove that specific node as well.

To adapt your existing method, you need to do something like:

foreach (XmlNode chkNode in nodes)
{
string currentName = "Test 1";
if (!nameDict.ContainsKey(currentName))
{
XmlNode parent = chkNode.ParentNode;
parent.ParentNode.RemoveChild(parent);
}
}

Of course, I can't test this as-is since I am not sure what exactly nameDict and nodes are.

For the sake of completeness, this code will break if chkNode or chkNode.ParentNode are XmlNodes that cannot have parents (such as a Attribute, Document, DocumentFragment, Entity, Notation nodes). If you think this is a possible scenario, you might want to include the appropriate null checks or wrap this method in a try-catch

Something like:

XmlNode parent = chkNode.ParentNode;
if (parent != null && parent.ParentNode != null)
parent.ParentNode.RemoveChild(parent);

Or:

try
{
XmlNode parent = chkNode.ParentNode;
parent.ParentNode.RemoveChild(parent);
}
catch (NullReferenceException ex)
{
// do something with exception
}

There are several options with LINQ to accomplish what you want in a better way, but since you are using C#2.0, this solution should get you in the right direction.



Related Topics



Leave a reply



Submit