Help with consolidating all xml files in a Folder using vb.net

Hi,

Ho do I consolidate multiple xml files in a folder to one xml file. For example if I have a "Forms" folder in my application's folder containing the following files:


M11File.xml

<Root>
<Table>
<ID>1</ID>
<ItemA>DataA<ItemA>
<ItemB>DataA<ItemB>
<ItemC>DataA<ItemC>
</Table>
</Root>

M33File.xml

<Root>
<Table>
<ID>1</ID>
<ItemA>DataA1<ItemA>
<ItemB>DataA1<ItemB>
<ItemC>DataA1<ItemC>
</Table>
</Root>

How do I obtain the file below?

MergeFile:

<Root>
<Table>
<ID>1</ID>
<ItemA>DataA<ItemA>
<ItemB>DataA<ItemB>
<ItemC>DataA<ItemC>
<Table>
<ID>1</ID>
<ItemA>DataA1<ItemA>
<ItemB>DataA1<ItemB>
<ItemC>DataA1<ItemC>
</Table>
</Root>


Thanks,

Victor
vcharlesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

ste5anSenior DeveloperCommented:
Why? The consuming program would no longer work.. have you test it?

btw, it should imho be  

<Root>
  <Tables>
    <Table FileName="M11File.xml">..</Table>
    <Table FileName="M33File.xml">..</Table>
  </Tables>
</Root>

Open in new window


Otherwise it's:

 
Module Module1

    Sub Main()

        Dim file1 As XDocument =
            <?xml version="1.0"?>
            <Root>
                <Table>
                    <ID>1</ID>
                    <ItemA>DataA</ItemA>
                    <ItemB>DataA</ItemB>
                    <ItemC>DataA</ItemC>
                </Table>
            </Root>

        Dim file2 As XDocument =
            <?xml version="1.0"?>
            <Root>
                <Table>
                    <ID>2</ID>
                    <ItemA>DataA</ItemA>
                    <ItemB>DataA</ItemB>
                    <ItemC>DataA</ItemC>
                </Table>
            </Root>

        Dim merged As XDocument = XDocument.Parse("<Root/>")

        merged.Root.Add(file1.Descendants("Table"))
        merged.Root.Add(file2.Descendants("Table"))

        Console.WriteLine(merged.ToString)
        Console.WriteLine("Done.")
        Console.ReadLine()

    End Sub

End Module

Open in new window

0
Lokesh B RDeveloperCommented:
Hi,

Use DataSet to combine the files into one.

The following code will take all the files from Folder "Forms" and merger it to one file.

VB.NET Code

Dim files As String() = System.IO.Directory.GetFiles(Server.MapPath("~/Forms/"))

If files IsNot Nothing AndAlso files.Count() > 0 Then
	Dim ds As New DataSet()

	For Each fileName As String In files
		Dim xmlreader1 As New XmlTextReader(fileName)
		ds.ReadXml(xmlreader1)
		ds.Merge(ds)
	Next
	ds.WriteXml(Server.MapPath("~/Forms/MergeFile.xml"))
End If

Open in new window


C# Code

 
string[] files = System.IO.Directory.GetFiles(Server.MapPath("~/Forms/"));

            if (files != null && files.Count() > 0)
            {
                DataSet ds = new DataSet();

                foreach (string fileName in files)
                {
                    XmlTextReader xmlreader1 = new XmlTextReader(fileName);
                    ds.ReadXml(xmlreader1);
                    ds.Merge(ds);
                }
                ds.WriteXml(Server.MapPath("~/Forms/MergeFile.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:
Thank You
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.