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

asked on

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
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

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?
Avatar of Victor  Charles

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America 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
Hi Fernando,
I will get back to you as soon as get a chance  to test it when i get home.
Thanks,
.victor
Hi Fernando,

I am matching by NSN not SN.

Thank You.

Victor
Thank you.