Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

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
Avatar of ste5an
ste5an
Flag of Germany image

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

ASKER CERTIFIED SOLUTION
Avatar of Lokesh B R
Lokesh B R
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Victor  Charles

ASKER

Thank You