Help with creating new file with dataelements in file1 only existing in File2 using VB.NET


  How do I add all data elements in File2 to File1. For example if  File1 contains:

   and File2 contains:


   How do I create the following file:



Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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;

See if this works for you.
'' Load the XML document into memory
Dim xdoc = XDocument.Load("C:\Working Directory\VictorFile1a.xml")
'' Load the XML document to search file1 to find nodes to add to new file1
Dim withTheseNodes = XDocument.Load("C:\Working Directory\VictorFile2a.xml")

'' Find all the XML nodes to be added to the new document
Dim results = (From n In xdoc.Descendants("Table1")
               From an In withTheseNodes.Descendants("Table2").Elements()
               Where n.Element(an.Name.LocalName) IsNot Nothing
               Select Element = n.Element(an.Name.LocalName), ID = n.Element("ID").Value).ToList()

'' Set up the new document to be created
Dim xdoc2 As New XDocument(New XElement("Root"))
For Each table As XElement In xdoc.Descendants("Table1")

'' Get a list of nodes to be added to the new document
Dim results2 = (From n In xdoc2.Descendants("Table1").Elements()
                Select n).ToList()
'' Remove all elements that were add to the new document
'' so that the right nodes can be added
For Each node As XElement In results2
    If node.Name.LocalName.Equals("ID") Then Continue For

'' Add the nodes to the new document
For Each nodePare In results
    Dim tableRef = (From n In xdoc2.Descendants("Table1")
                    Where n.Element("ID").Value = nodePare.ID
                    Select n).SingleOrDefault()

'' Sabe the new document
xdoc2.Save("Path and filename goes here")

Open in new window

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
vcharlesAuthor Commented:
Thanks, will try it when home later, just  realised other post related to delete data element is same as this post but was worded differently.

vcharlesAuthor Commented:
Hi Fernando,

It works.

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