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?

' 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")

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:
Hi Victor;

I think this should work for you.

Dim results = From n1 In f1.Descendants("Table1") _
              Where n1.Element("SN") IsNot Nothing _
              From n2 In f2.Descendants("Table1") _
              Where n2.Element("SN") IsNot Nothing AndAlso n1.Element("SN").Value = n2.Element("SN").Value _
              Select n1, n2

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
Fernando SotoRetiredCommented:
Hi Victor;

Is this solution working for you.
0
vcharlesAuthor Commented:
Hi Fernando,

I did not get a chance to test it yet, will get back to you later.

Thanks

Victor
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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