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

Hi,

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


   <Table1>
   <ID>1</ID>
   <DGG>C</DGG>
   <NAS>D</NAS>
  <NASC>MM</NASC>
   </Table1>
   <Table1>
   <ID>2</ID>
   <DGG>C1</DGG>
   <NAS>D1</NAS>
 <NASC>NN</NASC>
   </Table1>
 
   and File2 contains:

   <Table2>
   <NASC></NASC>
   </Table2>

   How do I create the following file:

   <Table1>
   <ID>1</ID>
  <NASC>MM</NASC>
   </Table1>
   <Table1>
   <ID>2</ID>
 <NASC>NN</NASC>
   </Table1>

   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;

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")
    xdoc2.Root.Add(table)
Next

'' 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
    node.Remove()
Next

'' 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()
    tableRef.Add(nodePare.Element)
Next

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

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
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.

Victor
0
vcharlesAuthor Commented:
Hi Fernando,

It works.

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