[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help with copying data from file2 to fil1 using vb.net

Posted on 2014-08-01
7
Medium Priority
?
172 Views
Last Modified: 2015-01-28
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
0
Comment
Question by:vcharles
  • 4
  • 3
7 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 40235611
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40243605
Hi Victor;

Is this solution working for you.
0
 

Author Comment

by:vcharles
ID: 40244457
Hi Fernando,

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

Thanks

Victor
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Closing Comment

by:vcharles
ID: 40247658
Thank You.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40247685
Not a problem Victor, glad to help.
0
 

Author Comment

by:vcharles
ID: 40576291
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40576655
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question