Solved

Help with creating list of all data elements from File1 to File2 using VB.NET

Posted on 2015-01-29
5
83 Views
Last Modified: 2015-01-30
Hi,

If I have an xml file with the following data:

File1.

<Root>
<Table>
<A><A>
<B></B>
</Table>
<Table>
<C><C>
<D></D>
</Table>
<Table>
<F><F>
<G></G>
</Table>
</Root>


How do I create File2 with all the data element in one table?

Desired output:

File2

<Root>
<Table>
<A><A>
<B></B>
<C><C>
<D></D>
<F><F>
<G></G>
</Table>
</Root>

Thanks,

Victor
0
Comment
Question by:vcharles
  • 2
  • 2
5 Comments
 
LVL 22

Expert Comment

by:rspahitz
ID: 40578759
If your example is correct, it might be better to look at it as a text problem rather than XML.
Open the xml file, remove all references to
"newline
</Table>newline
<Table>"

and save the file back.

If there are other things that might get in the way of that, you should be able to load the XML parser and navigate through nodes and rebuild (but there may be a better way that another expert can help with.)
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40578830
Hi Victor;

This will do what you want.

'' Load File1.xml into the XDocument
Dim xdoc As XDocument = XDocument.Load("C:\Working Directory\File1.xml")
'' Create the new XDocument to hold all elements of File1.xml
Dim xdocNew As New XDocument(<Root><Table></Table></Root>)

'' Get a reference to all table nodes
Dim addElements As List(Of XElement) = _
    (From ele In xdoc.Root.Descendants("Table") _
     From child In ele.Elements() _
     Select child).ToList()

'' Add all child elements of Table node to the new document
addElements.ForEach(Sub(c) xdocNew.Root.Element("Table").Add(c))

'' Save the new document
xdocNew.Save("C:\Working Directory\File1New.xml)

Open in new window

0
 

Author Comment

by:vcharles
ID: 40578847
Thank You. I will get back to you tomorrow  morning.
Victor
0
 

Author Closing Comment

by:vcharles
ID: 40580476
Thank You!
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40581027
Not a problem Victor, always glad to help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now