Help ith inputing data in file1 from file2 based on identical ID

Hi,

How do you modify the code below to input data in existing data element in file1 when both have identical NSN, code below adds a new record to file1 but I only need to copy the data from file2 to existing matching data elements in file1.

' Load the file1.xml and file2.xml into memory
        Dim f1 = XDocument.Load(Application.StartupPath + "\AOP40XYZ.xml")
        Dim f2 = XDocument.Load(Application.StartupPath + "\AOP40XYZA.xml")

        ' Query both documents and Join on the SN node in each 
        Dim results = From n1 In f1.Descendants("AOP40") _
                      Where n1.Element("NSN") IsNot Nothing _
                      From n2 In f2.Descendants("AOP40") _
                      Where n2.Element("NSN") IsNot Nothing AndAlso n1.Element("ID").Value = n2.Element("ID").Value _
                      Select n1, n2

        ' Copy the elements from File2 to File1
        For Each pair In results
            Dim removeSN = pair.n2.Element("SN")
            Dim removeID = pair.n2.Element("ID")
            removeSN.Remove()
            removeID.Remove()
            For Each element As XElement In pair.n2.Descendants()
                pair.n1.Add(element)
            Next
        Next

        '' Save the modified document
        f1.Save(Application.StartupPath + "\AOP40XYZFinal.xml")

Open in new window


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:
What do you need to do if the element in file 2 does NOT exist in file 1? Add it to file 1? Don't add it at all?
0
vcharlesAuthor Commented:
I don't  want to add it all, but would like to know that option if not too much to also include in the solution, incase my requirements change.
Thank You for all your Help.
Victor
0
Fernando SotoRetiredCommented:
Hi Victor;

Your code has been modified as per your request.

On line 7 in your code below you are matching on node name "NSN" but in the first For Each loop line 14 and 16 you are modifying on node name "SN" is that correct?

'' Load the file1.xml and file2.xml into memory
Dim f1 = XDocument.Load(Application.StartupPath + "\AOP40XYZ.xml")
Dim f2 = XDocument.Load(Application.StartupPath + "\AOP40XYZA.xml")

'' Query both documents and Join on the SN node in each 
Dim results = From n1 In f1.Descendants("AOP40") _
              Where n1.Element("NSN") IsNot Nothing _
              From n2 In f2.Descendants("AOP40") _
              Where n2.Element("NSN") IsNot Nothing AndAlso n1.Element("ID").Value = n2.Element("ID").Value _
              Select n1, n2

'' Copy the elements from File2 to File1
For Each pair In results
    Dim removeSN = pair.n2.Element("SN")
    Dim removeID = pair.n2.Element("ID")
    removeSN.Remove()
    removeID.Remove()
    For Each element As XElement In pair.n2.Descendants()
        Dim findElement As XElement = pair.n1.Element(element.Name.ToString())
        If findElement IsNot Nothing Then
            '' Element found in file 1, update its value
            findElement.Value = Element.Value
        Else
            '' Element was not found so do nothing. But if you want to add it uncomment next line
            '' pair.n1.Add(element)
        End If
    Next
Next

'' Save the modified document
f1.Save(Application.StartupPath + "\AOP40XYZFinal.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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

vcharlesAuthor Commented:
Hi Fernando,
I will get back to you as soon as get a chance  to test it when i get home.
Thanks,
.victor
0
vcharlesAuthor Commented:
Hi Fernando,

I am matching by NSN not SN.

Thank You.

Victor
0
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
Visual Basic.NET

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.