Solved

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

Posted on 2015-01-29
5
95 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 63

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 63

Expert Comment

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

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

713 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