删除整个父节点

时间:2015-04-13 19:11:15

标签: xml vba parent-child nodes

我有一个XML文件,其中包含多个名称和相关信息:

<Male>
<LastName LName="Smith">
  <FirstName FName="Robert">
   <DateOfBirth DOB="9/15/1980">
      <MiddleName>Thomas</MiddleName>
      <Alias>bob</Alias>
      <Address>1234 S. Street Ave</Address>
   </DateOfBirth>
   </FirstName>
</LastName>
<LastName LName="Smith">
      <FirstName FName="Ted">
       <DateOfBirth DOB="7/21/1977">
          <MiddleName>James</MiddleName>
          <Alias>T</Alias>
          <Address>1234 N. Avanue St</Address>
       </DateOfBirth>
       </FirstName>
    </LastName>

比方说,我想删除特德史密斯,但保留罗伯特。

我使用的代码如下:

ind = ListBox1.Value 'Gets Lastname
ind2 = Me.ListBox1.Column(1, Me.ListBox1.ListIndex) 'Gets Firstname
ind3 = Me.ListBox1.Column(2, Me.ListBox1.ListIndex) 'Gets DOB

Set Remo = objDom.SelectNodes("//LastName[@LName='" & ind & "']/FirstName[@FName='" & ind2 & "']/DateOfBirth[@DOB='" & ind3 & "']")
For Each RemoG In Remo
RemoG.ParentNode.RemoveChild RemoG
Next

这将删除DateOfBirth及其下的所有内容,但它会保留FirstName和LastName信息。如何在不删除其他Smith的情况下将其删除?

1 个答案:

答案 0 :(得分:0)

你需要导航&#34; up&#34;到<Male>并从那里删除

RemoG.ParentNode.ParentNode.ParentNode.RemoveChild RemoG.ParentNode.ParentNode