• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

Help with renaming and deleting tables in an xml file using VB.NET

Hi,

How do you rename a table and also delete a particular table? Foe example if I have

<Root>
 -<fields xmlns:xfdf="http://ns.adobe.com/xfdf-transition/">
<IemA>
<IemB>
</fields>
-<fNSC>
<IemA>
</NSC>
</Root>

How do I obtain

<Root>
 -<fiels>
<IemA>
<IemB>
</fields>
</Root>
0
vcharles
Asked:
vcharles
  • 4
  • 3
1 Solution
 
Fernando SotoCommented:
Hi Victor;

The XML structure given under the title "How do I obtain" is invalid. For example <fiels> has no end tag and </fields> has no begin tag. Also <IemA> and <IemB> have no end tag or did you mean to have this <IemA /> and <IemB />. Also are all these nodes to have no data associated with them?

<Root>
  <fiels>
    <IemA>
    <IemB>
  </fields>
</Root>

Open in new window

0
 
vcharlesAuthor Commented:
Hi Fernando,

I will send you another example shortly
Yes, the data elements will contain data.
Thsnks
Victor
0
 
vcharlesAuthor Commented:
From:

<Root>
  -<fields xmlns:xfdf="http://ns.adobe.com/xfdf-transition/">
 <IemA>xxxx</ItemA>
 <IemB>yyyy</ItemB>
 </fields>
 -<NSC>
 <IemA>ggggg</ItemA>
 </NSC>
 </Root>

 How do I obtain

<Root>
  <fields>
 <IemA>xxxx</ItemA>
 <IemB>yyyy</ItemB>
 </fields>
 </Root>

Thanks.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Fernando SotoCommented:
Hi Victor;

The following code snippet should do what you need.

'' Load XML document
Dim xdoc = XDocument.Load("From Path and XML File Name")

'' Find all nodes that do not have the tag name fields.
Dim results As List(Of XElement) = (From n In xdoc.Root.Elements() _       
                                    where Not (n.Name = "fields") _        
                                    Select n).ToList()        
             
'' Remove all the nodes in the list                                                                           
results.ForEach( Sub(n) n.Remove() )
'' Remove attribute from fields node                                       
xdoc.Root.Elements("fields").ToList().ForEach( Sub(a) a.RemoveAttributes())
'' Save the document
xdoc.Save("To Path and XML File Name")                                                

Open in new window

0
 
vcharlesAuthor Commented:
Thank you, I will try it later and get back to you.

Victor
0
 
vcharlesAuthor Commented:
Thank You.
0
 
Fernando SotoCommented:
Not a problem Victor, glad to help.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now