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?
 
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
 
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
 
vcharlesAuthor Commented:
Thank You
0
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.

All Courses

From novice to tech pro — start learning today.