Delete element from xml file, based on sub-element

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • veenna
    New Member
    • Dec 2007
    • 65

    Delete element from xml file, based on sub-element

    hi all,


    I have this XML file

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <Settings>
      <MapInfo>
        <text>item1</text>
        <positionLeft>55.426666259765625</positionLeft>
        <positionTop>163.94999694824219</positionTop>
        <original>item1</original>
      </MapInfo>
      <MapInfo>
        <text>item2</text>
        <positionLeft>103.42666625976563</positionLeft>
        <positionTop>110.94999694824219</positionTop>
        <original>name</original>
      </MapInfo>
    </Settings>
    How do i remove mapinfo node with all its sub nodes based on 'text' element it has?

    I tried something like this

    Code:
    xDocument.Descendants().Descendants("MapInfo").Descendants().Where(e => e.Value == "item1").Remove();
    it just removed text node from mapinfo.

    Any idea?


    Regards
    Veena
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    because you call .Remove() on that text node. you have to climb up to <MapInfo> and call .Remove() there.

    Comment

    • Monomachus
      Recognized Expert New Member
      • Apr 2008
      • 127

      #3
      Originally posted by veenna
      hi all,


      I have this XML file

      Code:
      <?xml version="1.0" encoding="utf-8"?>
      <Settings>
        <MapInfo>
          <text>item1</text>
          <positionLeft>55.426666259765625</positionLeft>
          <positionTop>163.94999694824219</positionTop>
          <original>item1</original>
        </MapInfo>
        <MapInfo>
          <text>item2</text>
          <positionLeft>103.42666625976563</positionLeft>
          <positionTop>110.94999694824219</positionTop>
          <original>name</original>
        </MapInfo>
      </Settings>
      How do i remove mapinfo node with all its sub nodes based on 'text' element it has?

      I tried something like this

      Code:
      xDocument.Descendants().Descendants("MapInfo").Descendants().Where(e => e.Value == "item1").Remove();
      it just removed text node from mapinfo.

      Any idea?


      Regards
      Veena
      Code:
      xDocument.Descendants("MapInfo").Descendants("text").FirstOrDefault(el => el.Value == "item1").Parent.Remove();

      Comment

      • Monomachus
        Recognized Expert New Member
        • Apr 2008
        • 127

        #4
        Originally posted by Monomachus
        Code:
        xDocument.Descendants("MapInfo").Descendants("text").FirstOrDefault(el => el.Value == "item1").Parent.Remove();
        What I made there is selecting first the element <text>item1</text> and than selecting his parent, which obviously is MapInfo, and remove it.

        Comment

        • drhowarddrfine
          Recognized Expert Expert
          • Sep 2006
          • 7434

          #5
          I don't understand. That's not XML or XSL or XPath or anything I've seen.

          Comment

          • Monomachus
            Recognized Expert New Member
            • Apr 2008
            • 127

            #6
            Originally posted by drhowarddrfine
            I don't understand. That's not XML or XSL or XPath or anything I've seen.
            That's LINQ to XML. It is a .NET thing. Let's say it's a "framework" to deal with different kind-of lists. It is used with Collections, SQL and XML in .NET.

            More information here - LINQ to XML

            Comment

            • drhowarddrfine
              Recognized Expert Expert
              • Sep 2006
              • 7434

              #7
              Ok. Didn't know this was a Windows only question.

              Comment

              Working...