Solved

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

Posted on 2015-01-29
5
96 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

732 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