Help with removing duplicates in string using VB.NET

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
vcharlesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

xav056Commented:
XmlDoc.DocumentElement("ctry").InnerText =
string.Join(",", XmlDoc.DocumentElement("ctry").InnerText.Split(',').Distinct().ToArray());
0
xav056Commented:
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
vcharlesAuthor Commented:
H

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

Thanks.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Fernando SotoRetiredCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Thanks, I will try it and get back to you.

victor
0
vcharlesAuthor Commented:
Thank You.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.