Solved

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

Posted on 2016-11-25
12
29 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 17

Expert Comment

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

Author Comment

by:vcharles
Comment Utility
Can you pleaze send me the source code.
Victor
0
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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
Comment Utility
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 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:vcharles
Comment Utility
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
Comment Utility
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
Comment Utility
Thank You.
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem Victor, glad to help.
0
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
@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
Comment Utility
Hi Pawan,

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

Thanks,

Victor
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

9 Experts available now in Live!

Get 1:1 Help Now