Help with merging multiple files in file1 based on identical values in file2 using VB.NET

Hi,

If file1.xml contains:

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <AOP6A>
    <SN>10412</SN>
    <CHPT>104</CHPT>
    <CA></CA>
</AOP6A>
  <AOP6A>
    <SN>10471</SN>
    <CHPT>105</CHPT>
    <CA></CA>
  </AOP6A>
</Root>

and file2.xml contains:

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <AOP5>
    <SN>10412</SN>
    <CHPT>104</CHPT>
    <Ctry>BEL</Ctry>
</AOP5>
  <AOP5>
    <SN>10412</SN>
    <CHPT>105</CHPT>
    <Ctry>FRA</Ctry>
  </AOP6A>

How do I enter Ctry values to iCA element in file1 based on matching SN, for example above, file1.xml should be as follows:

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <AOP6A>
    <SN>10412</SN>
    <CHPT>104</CHPT>
    <CA>BEL,FRA</CA> 'Data from file2 based on SN = 10412 in both files
</AOP6A>
  <AOP6A>
    <SN>10471</SN>
    <CHPT>105</CHPT>
    <CA></CA>
  </AOP6A>
</Root>
 
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;

This should do what you need.

'' Load the XML files
Dim xdoc1 = XDocument.Load("...\file1.xml")
Dim xdoc2 = XDocument.Load("...\file2.xml")

'' Query both documents and return values to be updated from file2
Dim results = (From n1 In xdoc1.Descendants("SN") _
               Join n2 In xdoc2.Descendants("SN") On n1.Value Equals  n2.Value _
               Where n2.Parent.Element("Ctry") IsNot Nothing AndAlso Not String.IsNullOrEmpty(n2.Parent.Element("Ctry").Value) _ 
               Select _
                   parent = n1.Parent, _
                   n1, _
                   n2, _
                   Ctry = n2.Parent.Element("Ctry").Value _
               ).ToList()

'' Modify file1 with the data from file2
For Each nodes in results
    If nodes.parent.Element("CA") Is Nothing Then    
        nodes.parent.Add(new XElement("CA", nodes.Ctry))
    Else
        Dim ca = nodes.parent.Element("CA").Value
        If String.IsNullOrEmpty(ca) Then
            nodes.parent.Element("CA").Value = nodes.Ctry
        Else
            nodes.parent.Element("CA").Value += "," + nodes.Ctry
        End If
    End If
Next

xdoc1.Save("... .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:
Thanks, will try it and get back to you.
0
vcharlesAuthor Commented:
Hi Fernando,

How do you modify the code to remove duplicates in <CA> and also sort the data.

Thank You.

victor
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

vcharlesAuthor Commented:
Hi,

I am able to sort the data In CA, but how do you remove or avoid duplicates in <CA>.

Thanks,

Victor
0
vcharlesAuthor Commented:
Hi,

The solution for the second question in in another post.

Thanks,

victor
0
vcharlesAuthor Commented:
Thank You!
0
Fernando SotoRetiredCommented:
Not a problem Victor, glad 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.