Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Help with sorting data element from xml file

Posted on 2014-08-04
5
Medium Priority
?
196 Views
Last Modified: 2014-08-04
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
0
Comment
Question by:vcharles
  • 2
  • 2
5 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 40239065
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
 
LVL 36

Expert Comment

by:ste5an
ID: 40239108
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
 

Author Comment

by:vcharles
ID: 40239130
Hi,

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

Victor
0
 

Author Closing Comment

by:vcharles
ID: 40239535
Thank You1
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40239796
Not a problem Victor, glad to help.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

581 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