Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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