Search code examples
xmlvb.netlinq-to-xmlxelement

How do delete XELEMENT in XDocument?


  <Settings>
    <List>
      <Holder>
        <Name>Peter</FolderName>
        <Age>35</Age>
      </Holder>
      <Holder>
        <Name>John</FolderName>
        <Age>25</Age>
      </Holder>
      <Holder>
        <Name>Micheal</FolderName>
        <Age>45</Age>
      </Holder>
      <Holder>
        <Name>Kenin</FolderName>
        <Age>28</Age>
      </Holder>
    </List>
  </Settings>

how to remove the "Kenin" holder completely below code is only remove the Name but i want to remove the kenin holder completely

            Dim e = From element In _xdoc.Descendants("Name") Where element.Value = "Kenin" Select element
        e.Remove()

Solution

  • Select parent element which should be wrapping element for name:

    Dim e = From element In _xdoc.Descendants("Name") Where element.Value = "Kenin" Select element.Parent
    e.Remove()