Solved

Help with copying data from file2 to file1 only when data element in File1 is empty using VB.NET

Posted on 2016-10-12
2
51 Views
Last Modified: 2016-10-14
How do I copy data from File2 to File1 for matching condition (Receiver and AGD values are identical in both files) only when data elements in file1 are empty and save the results in File3? I'm trying to avoid copying over existing data in file1. Also is there a way to avoid hard coding the data elements? my actual project includes much more data elements, if at all possible, would be best if I can avoid hard coding them.

File1.xml
 <?xml version="1.0" encoding="utf-8"?>
 <Root>
 <Table1>
   <Link_ID>1</Link_ID>
   <Receiver>USA</Receiver>
   <AGD>XXX</AGD>
   <NSC>FFF</NSC>
   <FCT>UUU</FCT>
 </Table1>
 <Table1>
   <Link_ID>2</Link_ID>
   <Receiver>BEL</Receiver>
   <AGD></AGD>
   <NSC></NSC>
   <FCT>HHH</FCT>
 </Table1>
 <Table1>
 </Root>

File2.xml
 <?xml version="1.0" encoding="utf-8"?>
 <Root>
 <Table2>
   <Link_ID>1</Link_ID>
   <Receiver>USA</Receiver>
   <AGD>XXX</AGD>
   <NSC>YYY</NSC>
   <FCT>LLLL</FCT>
 </Table2>
 <Table2>
   <Link_ID>2</Link_ID>
   <Receiver>BEL</Receiver>
   <AGD>XXX</AGD>
   <NSC>OOO</NSC>
   <FCT>XXX</FCT>
 </Table2>
 <Table2>
 </Root>


File3.xml
 <?xml version="1.0" encoding="utf-8"?>
 <Root>
 <Table1>
   <Link_ID>1</Link_ID>
   <Receiver>USA</Receiver>
   <AGD>XXX</AGD>
   <NSC>FFF</NSC>
   <FCT>UUU</FCT>
 </Table1>
 <Table1>
   <Link_ID>2</Link_ID>
   <Receiver>BEL</Receiver>
   <AGD>OOO</AGD>
   <NSC>XXX</NSC>
   <FCT>HHH</FCT>
 </Table1>
 <Table1>
 </Root>
0
Comment
Question by:vcharles
2 Comments
 
LVL 50

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41841428
since you mentioned the criteria of Receiver and AGD for comparison, hence somewhere in your codes you need to do comparison with these 2 elements.

you got to hard code it in your code, or you would need to have another configuration files to setup what elements you would like to compare in your program. for this second option, that could be much more work need to be done.
0
 

Author Comment

by:vcharles
ID: 41841440
Hi,

Below is the code I'm using but would like to avoid hard coding the names of the data elements. How do I modify the code to avoid that? If easier, I can also include the name of all the fields in a string variable (i.e. s = NSC, AGD, RECEIVER,....)


   If ComboBox3.SelectedIndex = 1 Then
            Dim x1 As New XmlDocument, x2 As New XmlDocument, x3 As New XmlDocument
            x1.Load("File1.xml")
            x2.Load("File2.xml")
            Dim x As XmlElement = x1.DocumentElement.FirstChild
            While x IsNot Nothing ' loop over all Table1 nodes
                ' check if AGD node exists under current Table1 node
                Dim sn1 As XmlElement = x.SelectSingleNode("RECEIVER")
                Dim sn1A As XmlElement = x.SelectSingleNode("AGD")
                If sn1 IsNot Nothing Then
                    ' check if there is text in the AGD node
                    Dim sn1t As String = sn1.InnerText
                    Dim sn1tA As String = sn1A.InnerText
                    If Not String.IsNullOrWhiteSpace(sn1t) Then
              Dim y As XmlElement = x2.SelectSingleNode("//Table2[RECEIVER='" & sn1t & "' and AGD ='" & sn1tA & "']")
                    If y IsNot Nothing Then
                        ' check if there is a new value present
                        Dim ric5 As XmlElement = y.SelectSingleNode("NSC")
                        If ric5 IsNot Nothing Then
                            ' check if RIC node exists under current Table1 node
                            Dim ric1 As XmlElement = x.SelectSingleNode("NSC")
                            If ric1 Is Nothing Then ' create it
                                ric1 = x.AppendChild(x1.CreateElement("NSC"))
                            End If
                            If ric1.InnerText = "" Then
                                ric1.InnerText = ric5.InnerText
                            End If
                        End If

                        Dim ric6 As XmlElement = y.SelectSingleNode("FCT")
                        If ric6 IsNot Nothing Then
                            ' check if RIC node exists under current Table1 node
                            Dim ric1 As XmlElement = x.SelectSingleNode("FCT")
                            If ric1 Is Nothing Then ' create it
                                ric1 = x.AppendChild(x1.CreateElement("FCT"))
                            End If
                            If ric1.InnerText = "" Then
                                ric1.InnerText = ric6.InnerText
                            End If
                        End If
                    End If
                End If
                End If

        x = x.NextSibling
            End While
            x1.Save("File3.xml")
        End If

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
in Dot net,lastest version of MVC 3 24
Alert on Server memory 2 22
Store results in vb.net 3 20
Please explain the parts of these 2 LINQ expressions 3 30
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

777 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