?
Solved

Help with merging all xml files in a folder

Posted on 2014-08-16
9
Medium Priority
?
135 Views
Last Modified: 2014-08-18
Hi,

How do you merge all xml files in an applications sub folder and save them as one file?

Thanks,

Victor
0
Comment
Question by:vcharles
  • 5
  • 4
9 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40265014
Hi Victor;

It depends on how you wish to merge the files. But if you just wish to concatenate all the files then select one file to concatenate all the other files into. Then from all the other files retrieve all elements between the root node and then append them and the end of the first file.
0
 

Author Comment

by:vcharles
ID: 40265021
Hi Fernando,

I would like to append without naming the files names since there will be many files in the folder. The goal is to merge all the tables in one file, than sort by tables from the single file.

Thanks,

Victor
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40265022
Please give a full description of what needs to be done and how the merge all the xml files.
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:vcharles
ID: 40265053
Hi,

For example, if I have the following folders in my data subfolder:

NSC.xml
<Root>
<Table_NSC>
<ID> 1</ID>
<NSC></NSC>
</Table_NSC>
</Root>

AGD.xml


<Root>
<Table_AGD>
<ID> 1</ID>
<AGD></AGD>
</Table_NSC>
</Root>

STR.xml

<Root>
<Table_NSC>
<ID> 1</ID>
<STR></STR>
</Table_STR>
</Root>

I would like to have Final.xml containing:

Final.xml
<Root>
<Table_NSC>
<ID> 1</ID>
<NSC></NSC>
</Table_NSC>
</Root>
<Table_AGD>
<ID> 1</ID>
<AGD></AGD>
</Table_NSC>
</Root>
<Table_NSC>
<ID> 1</ID>
<STR></STR>
</Table_STR>
</Root>

and sort the IDs within  each table. Since there wil be many xml files, I need to avoid specifying the names of the xml files.

Thanks

Victor
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40266113
Hi Victor;

A couple of questions please. First is in your example of Final.xml should contain you seem to have multiple Root nodes in the document which violate the rules of a Root node. Second the start tags and end tags are not all matching. Third you state that you want the ID nodes sorted because there will be many nodes but that means that within a <Table...> ... </Table...> you will have multiple single ID nodes something like this:
<Root>
    <Table_NSC>
        <ID> 1</ID>
        <ID> 2</ID>
        <ID> 3</ID>
        <NSC></NSC>
        <NSC></NSC>
        <NSC></NSC>
    </Table_NSC>
<!-- ... -->
</Root>

Open in new window

Is that correct or something else?
0
 

Author Comment

by:vcharles
ID: 40266237
Hi Fernando,

I'm sorry for the mistake, Final.xml should merge all the xml file and there should only be one <Root</Root>

Final.xml
 <Root>
 <Table_NSC>
 <ID> 1</ID>
 <NSC></NSC>
 </Table_NSC>
 <Table_AGD>
 <ID> 1</ID>
 <AGD></AGD>
 <Table_AGD>
 <Table_STR>
 <ID> 1</ID>
 <STR></STR>
 </Table_STR>
 </Root>
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 40267968
Hi Victor;

Try it like this.

'' Data directory
Dim dataDir = "C:\Working Directory\TestData\"
'' Get a directory info object of the data directory
'' which will be used to get a list of files
Dim di As DirectoryInfo = new DirectoryInfo(dataDir)
'' Get the list of XML dicuments in the data directory
Dim files As List(Of FileInfo) = di.GetFiles("*.xml").ToList()
'' Create a XML document to hold the concatenation of all the other XML documents.
Dim xdoc As XDocument = new XDocument(new XElement("Root"))

'' Concatenate the XML files
For Each fi As FileInfo In files
    Dim xmldoc As XDocument = XDocument.Load(fi.FullName)
    xdoc.Root.Add(xmldoc.Root.Elements())
Next

'' Sort the nodes in the document by ID
Dim results = (From element In xdoc.Root.Elements()
               Order By CInt(element.Element("ID").Value)
               Select element).ToList()

'' Remove all unsorted nodex
xdoc.Root.RemoveAll()
'' Replace the nodes in sorted order
xdoc.Root.ReplaceAll(results)

'' Save the document to the file system.
xdoc.Save(dataDir + "ConcatenatedXML.xml")

Open in new window

0
 

Author Comment

by:vcharles
ID: 40269320
Hi Fernando,

It works.

Thank You.

Victor
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40269329
Not a problem Victor, glad to be able to help.
0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

839 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