如何在XDocument中删除XELEMENT?

时间:2016-12-06 16:09:35

标签: xml vb.net linq-to-xml xelement

  <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>

如何删除&#34;肯宁&#34;持有人完全 下面的代码只删除名称但我想删除kenin 持有人完全

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

2 个答案:

答案 0 :(得分:1)

选择应该包装名称元素的父元素:

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

答案 1 :(得分:0)

怎么样,

    'for testing
    Dim xe As XElement = <Settings>
                             <List>
                                 <Holder>
                                     <Name>Peter</Name>
                                     <Age>35</Age>
                                 </Holder>
                                 <Holder>
                                     <Name>John</Name>
                                     <Age>25</Age>
                                 </Holder>
                                 <Holder>
                                     <Name>Micheal</Name>
                                     <Age>45</Age>
                                 </Holder>
                                 <Holder>
                                     <Name>Kenin</Name>
                                     <Age>28</Age>
                                 </Holder>
                             </List>
                         </Settings>

    xe...<Name>.Where(Function(el) el.Value = "Kenin").Select(Function(el) el.Parent).Remove()