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
' Save file1.xml as file3.xml