Help with sorting data element from xml file

Hi,

 How would you load the xml file below and sort all the ctry data element?

 <Root>
 <Table1>
 <Item>1</Item>
 <Ctry>BEL,USA,CAN<Ctry>
 </Table1>
 <Table1>
 <Item>2</Item>
 <Ctry>BEL,USA,CAN<Ctry>
 </Table1>
 <Table1>
 <Item>3</Item>
 <Ctry>FRA,USA,CAN<Ctry>
 </Table1>
 <Table1>
 <Item>4</Item>
 <Ctry>USA,CAN,FRA,NLD<Ctry>
 </Table1>
 </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.

Fernando SotoRetiredCommented:
Hi Victor;

This should sort the cities in the Ctry nodes.

'' Load the XML document from the file system
Dim xdoc = XDocument.Load("... .xml")

'' Get a reference to all the Ctry nodes
Dim results = (From n In xdoc.Descendants("Ctry") _
               Select n).ToList()
               
'' Sort the cities in the Ctry nodes
For Each element As XElement In results
    Dim cities As List(Of string) = element.Value.Split(New char() {","c}).ToList()
    cities.Sort()
    element.Value = String.Join(",", cities)
Next

'' Save to the file system
xdoc.Save("... .xml")

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
ste5anSenior DeveloperCommented:
When the ctry values need to be sorted, then they seem important enough, the have their own element. E.g.
<Ctrys>
    <Ctry>FRA<Ctry>
    <Ctry>USA<Ctry>
    <Ctry>CAN<Ctry>
</Ctrys>

Open in new window

0
vcharlesAuthor Commented:
Hi,

Thanks for the code, will try it and get back to you.

Victor
0
vcharlesAuthor Commented:
Thank You1
0
Fernando SotoRetiredCommented:
Not a problem Victor, glad to help.
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.