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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

20 Experts available now in Live!

Get 1:1 Help Now