Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

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

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>
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Victor  Charles

ASKER

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