Solved

Help with deleting records with two duplicate columns using VB.NET

Posted on 2016-11-25
12
43 Views
Last Modified: 2016-11-27
Hi,

How do you delete duplicate records if SN and AGD have same information using VB.NET? For example if the following file contains:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<NewDataSet>
  <AOP6A>
    <SN>10411</SN>
    <AGD>CAR</AGD>
    <CHPT>104</CHPT>
    <RIC>DEALER</RIC>
  </AOP6A>
 <AOP6A>
  <SN>10411</SN>
    <AGD>CAR</AGD>
    <CHPT>104</CHPT>
    <RIC>DEALER</RIC>
  </AOP6A>
<AOP6A>
<SN>10412</SN>
    <AGD>CAR</AGD>
    <CHPT>104</CHPT>
    <RIC>DEALER</RIC>
  </AOP6A>
</NewDataSet>

I would like to have the following file:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<NewDataSet>
  <AOP6A>
    <SN>10411</SN>
    <AGD>CAR</AGD>
    <CHPT>104</CHPT>
    <RIC>DEALER</RIC>
  </AOP6A>
<AOP6A>
<SN>10412</SN>
    <AGD>CAR</AGD>
    <CHPT>104</CHPT>
    <RIC>DEALER</RIC>
  </AOP6A>
</NewDataSet>

Thanks,

Victor
0
Comment
Question by:vcharles
  • 5
  • 4
  • 3
12 Comments
 
LVL 25

Expert Comment

by:Pawan Kumar
ID: 41901405
DataSet ds
ds = new DataSet()
ds.ReadXml(xml file path)
use linq to filter duplicate rows..
0
 

Author Comment

by:vcharles
ID: 41901422
Can you pleaze send me the source code.
Victor
0
 
LVL 25

Expert Comment

by:Pawan Kumar
ID: 41901442
Try...

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim a = New DataSet()
        a.ReadXml("E:\pawan.xml")

        Dim b = a.Tables(0).AsEnumerable().[Select](Function(d) New With { _
         Key .SN = d.Field(Of String)("SN"), _
         Key .AGD = d.Field(Of String)("AGD"), _
         Key .CHPT = d.Field(Of String)("CHPT"), _
         Key .RIC = d.Field(Of String)("RIC") _
        }).Distinct().ToList()

        Console.WriteLine()

    End Sub

Open in new window

0
 

Author Comment

by:vcharles
ID: 41901459
Hi,

To avoid writing the fields names, is there a way to modify the code for identical records instead of the two fields?

Thanks,

Victor
0
 
LVL 25

Expert Comment

by:Pawan Kumar
ID: 41901540
Ok.. try

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim a = New DataSet()
        a.ReadXml("E:\pawan.xml")

        'Dim b = a.Tables(0).AsEnumerable().[Select](Function(d) New With { _
        ' Key .SN = d.Field(Of String)("SN"), _
        ' Key .AGD = d.Field(Of String)("AGD"), _
        ' Key .CHPT = d.Field(Of String)("CHPT"), _
        ' Key .RIC = d.Field(Of String)("RIC") _
        '}).Distinct().ToList()

        Dim k As DataTable
        k = a.Tables(0)
        Dim result = k.AsEnumerable().Distinct(System.Data.DataRowComparer.[Default]).ToList()


        Console.WriteLine()

    End Sub

Open in new window

0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41901796
Hi Victor;

The below code snippet should do what you need without using tag names.
Dim xdoc = XDocument.Load("C:\Working Directory\Victor.xml")

Dim results = (From node In xdoc.Root.Elements()
               Group node By node.Name.LocalName Into nodeGroup = Group
               From ng In nodeGroup
               Let nodeStr = ng.ToString().Replace(vbNewLine, "").Replace(" ", "")
               Select ng, nodeStr).GroupBy(Function(k) k.nodeStr).Select(Function(n) n.First().ng)

xdoc.Root.ReplaceAll(results)
xdoc.Save("C:\Working Directory\VictorUpdated.xml")

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:vcharles
ID: 41902233
Hi Fernando,

Thank you for the solution.

Hi Pawan,

Using your approach, how do you save the results to an xml file?

Thanks,

Victor
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41902304
Hi Victor;

The solution by Pawan does not meet the requirements as posted by not using Tag names such as SN, AGD, CHPT and  RIC.
0
 

Author Closing Comment

by:vcharles
ID: 41902418
Thank You.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41902428
Not a problem Victor, glad to help.
0
 
LVL 25

Expert Comment

by:Pawan Kumar
ID: 41902734
@Fernando Soto-
Have you seen my first solution? See below-  , I had used the tags, But later the Author told me not to use them.

Dim b = a.Tables(0).AsEnumerable().[Select](Function(d) New With { _
        Key .SN = d.Field(Of String)("SN"), _
         Key .AGD = d.Field(Of String)("AGD"), _
         Key .CHPT = d.Field(Of String)("CHPT"), _
         Key .RIC = d.Field(Of String)("RIC") _

        }).Distinct().ToList()
0
 

Author Comment

by:vcharles
ID: 41903101
Hi Pawan,

I wanted to compared two fields without including all the fields in my code.

Thanks,

Victor
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

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

863 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