Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. LINQ to xml query

LINQ to xml query

Scheduled Pinned Locked Moved C#
csharpdatabaselinqxml
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    faheemnadeem
    wrote on last edited by
    #1

    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

    realJSOPR 1 Reply Last reply
    0
    • F faheemnadeem

      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

      realJSOPR Offline
      realJSOPR Offline
      realJSOP
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups