Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help with removing duplicates in string using VB.NET

Posted on 2014-08-01
6
Medium Priority
?
219 Views
Last Modified: 2014-08-01
Hi,

I have an xml file which contains a data element with string of duplicate data.For example<CTRY>BEL,BEL,GRC,GRC,USA<CTRY>. How do I remove the duplicate data?

For example:

<Root>
<Table>
<Id>1</id>
<ctry>BEL,BEL,GRC,GRC,USA<CTRY</ctry>
<Item>BMW</item>
</Table>
<Table>
<Id>1</id>
<ctry>BEL,BEL,GRC,GRC,USA,USA<CTRY</ctry>
<Item>BMW</item>
</Table>
<Table>
<Id>1</id>
<ctry>BEL,DEU,GRC,BEL,USA<CTRY</ctry>
<Item>BMW</item>
</Table>
</Root>

Thanks,

Victor
0
Comment
Question by:vcharles
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:xav056
ID: 40235122
XmlDoc.DocumentElement("ctry").InnerText =
string.Join(",", XmlDoc.DocumentElement("ctry").InnerText.Split(',').Distinct().ToArray());
0
 
LVL 9

Expert Comment

by:xav056
ID: 40235124
not sure how you are parsing the xml but the idea is as ollows
dim duplicateString as string
duplicateString= "BEL,BEL,GRC,GRC,USA";

Dim cleanString as string = string.Join(",", duplicateString.Split(',').Distinct().ToArray())
0
 

Author Comment

by:vcharles
ID: 40235260
H

Once i load the file using xmldocument how do i use your code and save the changes?

Thanks.
0
Industry Leaders: 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!

 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 40235412
Hi Victor;

Thsi code snippet should give you what you need.

Dim inputXML As String = "File path and name"
Dim saveXML As String = "File path and name"
'' Load the XML document
Dim xdoc = XDocument.Load(inputXML)

Dim results As List(Of XElement) = (From n In xdoc.Descendants("ctry") _
                                    Select n).ToList()
                              
For Each node As XElement In results 
    Dim values As List(Of String) = node.Value.Split(New char() {","c}).Distinct().ToList()
    node.Value = String.Join(",", values)
Next

xdoc.Save(saveXML)

Open in new window

0
 

Author Comment

by:vcharles
ID: 40235449
Thanks, I will try it and get back to you.

victor
0
 

Author Closing Comment

by:vcharles
ID: 40235513
Thank You.
0

Featured Post

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!

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

810 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