Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

**Sorting XML Nodes (ABC) 500 points!**

Posted on 2004-10-30
8
Medium Priority
?
390 Views
Last Modified: 2010-04-23
ok... I need my XML document / visitors nodes sorted by uniqueSN... here what I have so far...

Here's my XML file so far...

- - - - - - - - - - >>
<Visitors>
  <visitor>
    <uniqueSN>testing</uniqueSN>
    <viewedSN>T E S Ting</viewedSN>
    <totalVisits>16</totalVisits>
    <lastVisit>10/30/2004 : 5:52:33 AM</lastVisit>
    <firstVisit>
    </firstVisit>
  </visitor>
</Visitors>

- - - - - - - - - - >>

Here's my VB.NET file so far...

- - - - - - - - - - >>
Dim viewedSN as String = Request.QueryString("ScreenName")
        Dim UniqueSN as String = CleanInput(viewedSN)
        Dim visitorDate as String = Request.QueryString("Date")
        Dim visitorTime as String = Request.QueryString("Time")
        Dim myScreenName as String = "myScreenName"
        Dim totalVisits as Integer

        Dim xmlPath As String = Server.MapPath("/Visitors.xml")
        Dim nList As XmlNodeList
        Dim n As XmlNode
        Dim nodeCount As Integer
        Dim myXMLdocument As XmlDocument = New XmlDocument()
        myXMLdocument.Load(xmlPath)

        nList = myXMLdocument.SelectNodes("Visitors/visitor")
        nodeCount = nList.Count()

        Dim foundSN As String = "no"

        If (nList.Count > 0) Then
            For Each n In nlist 'if name matches
                If (n.ChildNodes.Item(0).InnerText = uniqueSN) Then
                    n.ChildNodes.Item(1).InnerText = viewedSN
                    n.ChildNodes.Item(2).InnerText += 1
                    totalVisits = CInt(n.ChildNodes.Item(2).InnerText)
                    n.ChildNodes.Item(3).InnerText = visitorDate + " : " + visitorTime
                    foundSN = "yes"
                    Exit For
                End If
            Next
        End If

        If (foundSN = "no") then

            'building XML Nodes
            Dim startNode As XmlNode = myXMLdocument.selectSingleNode("//Visitors")

            Dim nVisitor As XmlElement = myXMLdocument.CreateElement("visitor")

            Dim nUniqueSN As XmlElement = myXMLdocument.CreateElement("uniqueSN")
            nUniqueSN.InnerText = (uniqueSN)

            Dim nViewedSN As XmlElement = myXMLdocument.CreateElement("viewedSN")
            nViewedSN.InnerText = (viewedSN)

            Dim nTotalVisits As XmlElement = myXMLdocument.CreateElement("totalVisits")
            nTotalVisits.InnerText = ("1")

            Dim nLastVisit As XmlElement = myXMLdocument.CreateElement("lastVisit")
            nLastVisit.InnerText = (visitorDate + " : " + visitorTime)

            Dim nFirstVisit As XmlElement = myXMLdocument.CreateElement("firstVisit")
            nFirstVisit.InnerText = (visitorDate + " : " + visitorTime)

            startNode.AppendChild(nVisitor)
            nVisitor.AppendChild(nUniqueSN)
            nVisitor.AppendChild(nViewedSN)
            nVisitor.AppendChild(nTotalVisits)
            nVisitor.AppendChild(nLastVisit)
            nVisitor.AppendChild(nFirstVisit)

        End If

        myXMLdocument.Save(xmlPath)

- - - - - - - - - - >>

Ok, now that you read all that, all I want to do is dort my XML document by uniqueSN.  When I create the new node / childs it adds it to the bottom and I would like the document sorted.  Can someone please help me?!

<< MM >>
0
Comment
Question by:Moonbathing
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 

Author Comment

by:Moonbathing
ID: 12453696
Can anyone help me do a simple sort?
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 600 total points
ID: 12454282
One way that you might try this is to add each node to a SortedList object, using the key necessary to sort in the correct order, and then read the entries from the SortedList.  NOTE:  This object will only work if you want ascending string sorts.

Bob
0
 
LVL 5

Accepted Solution

by:
thenrich earned 1400 total points
ID: 12464965
Put your xml doc into a dataset sort the dataset and re-populate your xml.

Tom
0
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!

 
LVL 96

Expert Comment

by:Bob Learned
ID: 12465926
Ooh, another great suggestion :)

Bob
0
 
LVL 5

Expert Comment

by:thenrich
ID: 12465953
'Ooh, another great suggestion :)'

I hopes your not being sarcastic Bob...
0
 

Author Comment

by:Moonbathing
ID: 12468175
How would I go about populating it into a data set for manipulation?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12468304
No, I was being very serious, Tom.  I hadn't thought about using a DataSet.ReadXML and DataSet.WriteXML.  Very cool :)  You just haven't been around my sense of humor enough, I guess :)

Bob
0
 
LVL 5

Expert Comment

by:thenrich
ID: 12484149
I know you were Bob. I was just mee'in around with you.

Moonbathing I'll put something together for 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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

636 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