Help with merging excel files based on identical IDs

Hi,

I'm exporting data from multiple C1TrueDBGrid controls to multiple excel files. How do I create 1 xml file from multiple xml files based on identical IDs using vb.net?  For example if first execel file contains:

id   nsn   add
1    888   hnm
2    999   ujk

and excel file 2 contains:

id   NAS  NMN
1    777   ccc
2    666   ddd

I would like to have the following xml file:

id   nsn   add     NAS   NMN
1    888   hnm    777    ccc
2    999   ujk      666    ddd


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.

Fernando SotoRetiredCommented:
Will the columns names always be the same for example will this always be the resulting Excel file column header row.

id   nsn   add     NAS   NMN
vcharlesAuthor Commented:
Hi Fernando.

Yes.

Victor
Fernando SotoRetiredCommented:
Hi Victor;

To your question, "How do I create 1 xml file from multiple xml files based on identical IDs using vb.net?",
'' Your First XML file
Dim doc1 As XDocument = XDocument.Load("C:\Working Directory\ExcelBookData1.xml")
'' Your Second XML file
Dim doc2 As XDocument = XDocument.Load("C:\Working Directory\ExcelBookData2.xml")
'' Resulting XML file
Dim doc12 As New XDocument(New XElement("Root"))

'' Join the row of the XML files on "ID" and creating a new XElement for each.
Dim result = (From d1 In doc1.Root.Descendants("Table1")
              Join d2 In doc2.Root.Descendants("Table1") On d1.Element("ID").Value Equals d2.Element("ID").Value
              Select New XElement("Table1",
                        New XElement("ID", d1.Element("ID").Value),
                        New XElement("ID", d1.Element("NSN").Value),
                        New XElement("ID", d1.Element("ADD").Value),
                        New XElement("ID", d2.Element("NAS").Value),
                        New XElement("ID", d2.Element("NMN").Value)
              )).ToList()

'' Add new elements to the new XDocument
result.ForEach(Sub(node) doc12.Root.Add(node))
'' Save the new XDocument
doc12.Save("Path on the File Syatem.xml")

Open in new window

The above code is assuming the following.
File 1 has this schema

<Root>
  <Table1>
    <ID>1</ID>
    <NSN>888</NSN>
    <ADD>hnm</ADD>
  </Table1>
  <Table1>
    <ID>2</ID>
    <NSN>999</NSN>
    <ADD>ujk</ADD>
  </Table1>
</Root>

File 2 has this schema

<Root>
  <Table1>
    <ID>1</ID>
    <NAS>777</NAS>
    <NMN>ccc</NMN>
  </Table1>
  <Table1>
    <ID>2</ID>
    <NAS>666</NAS>
    <NMN>ddd</NMN>
  </Table1>
</Root>

The resulting document will be

<?xml version="1.0"?>
<Root>
  <Table1>
    <ID>1</ID>
    <ID>888</ID>
    <ID>hnm</ID>
    <ID>777</ID>
    <ID>ccc</ID>
  </Table1>
  <Table1>
    <ID>2</ID>
    <ID>999</ID>
    <ID>ujk</ID>
    <ID>666</ID>
    <ID>ddd</ID>
  </Table1>
</Root>

Open in new window

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
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

vcharlesAuthor Commented:
Fernando,

I will try it and get back to you.

Thank You,

Victor
vcharlesAuthor Commented:
Hi Fernando,

I'm sorry, I meant to ask how do I create one excel file from multiple excel file, will pot another question.

Thanks,

Victor
vcharlesAuthor Commented:
Thank You.
vcharlesAuthor Commented:
Hi Fernando,

How would you modify the code below to work with three xml files? All three to be joined by ID.

Dim result = (From d1 In doc1.Root.Descendants("Table1")
              Join d2 In doc2.Root.Descendants("Table1") On d1.Element("ID").Value Equals d2.Element("ID").Value

If you prefer, I can open a new issue.

Thanks,

Victor
Fernando SotoRetiredCommented:
Victor, you need to open another question.
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.