Solved

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

Posted on 2016-11-25
12
51 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
[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
  • 5
  • 4
  • 3
12 Comments
 
LVL 28

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 28

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 28

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 63

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
 

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 63

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 63

Expert Comment

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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