Help with merging all xml files in a folder

Hi,

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

Thanks,

Victor
vcharlesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
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
vcharlesAuthor Commented:
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
Fernando SotoRetiredCommented:
Please give a full description of what needs to be done and how the merge all the xml files.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

vcharlesAuthor Commented:
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
Fernando SotoRetiredCommented:
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
vcharlesAuthor Commented:
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
Fernando SotoRetiredCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Hi Fernando,

It works.

Thank You.

Victor
0
Fernando SotoRetiredCommented:
Not a problem Victor, glad to be able to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.