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
63 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 51

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 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