LINQ to xml query
-
Hi, I am trying to delete some nodes from an xml document. I am using LINQ to xml and want to stick to this approach. Here is my XML document:
I have to perform two cases. 1. Delete clip node based on ID. 2. Clear whole project root tag. I am using the following code to delete nodes.
// Obtain the first clip entry
IEnumerable<XElement> clip = (from c in this.oProjectDoc.Element("Project").Element("Clips").Elements("Clip")
where c.Attribute("ID").Value.Equals(ID)
select c);// Delete clip entry foreach (XElement xe in clip) xe.Remove(); // Save updated options document to file system this.oProjectDoc.Save(this.oProjectPath.FullName);
Have tried this
-
Hi, I am trying to delete some nodes from an xml document. I am using LINQ to xml and want to stick to this approach. Here is my XML document:
I have to perform two cases. 1. Delete clip node based on ID. 2. Clear whole project root tag. I am using the following code to delete nodes.
// Obtain the first clip entry
IEnumerable<XElement> clip = (from c in this.oProjectDoc.Element("Project").Element("Clips").Elements("Clip")
where c.Attribute("ID").Value.Equals(ID)
select c);// Delete clip entry foreach (XElement xe in clip) xe.Remove(); // Save updated options document to file system this.oProjectDoc.Save(this.oProjectPath.FullName);
Have tried this
Instead of this:
var q = from node in oProjectDoc.Root.DescendantsAndSelf("Clips")
let attr = node.Attribute("ID")
where attr != null && attr.Value == clipId
select node;
q.ToList().ForEach(x => x.Remove());Why can't you do this:
var q = (from node in oProjectDoc.Root.DescendantsAndSelf("Clips")
let attr = node.Attribute("ID")
where attr != null && attr.Value != clipId
select node);
oProjectDoc.Root.RemoveAll();
foreach(XElement element in q)
{
oProjectDoc.Root.Add(element);
}".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997