Victor Charles
asked on
Help with copying data from file2 to fil1 using vb.net
Hi,
I am using the code below to save data from file1 to file2 based on identical SN values, but receiving error message "Object reference not set to an instance of an object." on line:
Join n2 In f2.Descendants("Table1") On n1.Element("SN").Value Equals n2.Element("SN").Value
and n2.Element("SN").Value is highlighted as specific cause of error.
which I think is because some records don't have any matching values, for example file1 may have an SN where there is no matching SN in file 2.
How do I avoid this error?
Thanks,
Victor
I am using the code below to save data from file1 to file2 based on identical SN values, but receiving error message "Object reference not set to an instance of an object." on line:
Join n2 In f2.Descendants("Table1") On n1.Element("SN").Value Equals n2.Element("SN").Value
and n2.Element("SN").Value is highlighted as specific cause of error.
which I think is because some records don't have any matching values, for example file1 may have an SN where there is no matching SN in file 2.
How do I avoid this error?
' Load the file1.xml and file2.xml into memory
Dim f1 = XDocument.Load("C:\Working Directory\file1.xml")
Dim f2 = XDocument.Load("C:\Working Directory\file2.xml")
' Query both documents and Join on the SN node in each
Dim results = From n1 In f1.Descendants("Table1") _
Join n2 In f2.Descendants("Table1") On n1.Element("SN").Value Equals n2.Element("SN").Value _
Select n1, n2
' Add the NSD node in file2.xml to the corresponding node in file1.xml
For Each pair in results
pair.n1.Add(pair.n2.Element("NSD"))
Next
' Save file1.xml as file3.xml
f1.Save("C:\Working Directory\file3.xml")
Thanks,
Victor
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Fernando,
I did not get a chance to test it yet, will get back to you later.
Thanks
Victor
I did not get a chance to test it yet, will get back to you later.
Thanks
Victor
ASKER
Thank You.
Not a problem Victor, glad to help.
ASKER
Hi,
How do I modify the code to copy multiple data elements listed in S = NSC,AGD,NSN from file2 to file1, instead of running the code separately for each values in data element to be added in file1? If preferred, I can open a new incident.
Thanks,
Victor
How do I modify the code to copy multiple data elements listed in S = NSC,AGD,NSN from file2 to file1, instead of running the code separately for each values in data element to be added in file1? If preferred, I can open a new incident.
Thanks,
Victor
Hi Victor;
Please do open a new question for this. When you open the new question please provide what XML document you are starting out with and what you need done and what the final results should look like.
Thanks;
Fernando
Please do open a new question for this. When you open the new question please provide what XML document you are starting out with and what you need done and what the final results should look like.
Thanks;
Fernando
Is this solution working for you.