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
44 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 49

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Populating an array of locale_time_info 8 37
C# Offline Apllication 5 53
Syntax Error 2 43
SQL Login 17 38
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now