Solved

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

Posted on 2015-01-29
5
79 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank You. I will get back to you tomorrow  morning.
Victor
0
 

Author Closing Comment

by:vcharles
Comment Utility
Thank You!
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem Victor, always glad to help.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

7 Experts available now in Live!

Get 1:1 Help Now