Link to home
Start Free TrialLog in
Avatar of Moonbathing
Moonbathing

asked on

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

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 >>
Avatar of Moonbathing
Moonbathing

ASKER

Can anyone help me do a simple sort?
SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ooh, another great suggestion :)

Bob
'Ooh, another great suggestion :)'

I hopes your not being sarcastic Bob...
How would I go about populating it into a data set for manipulation?
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
I know you were Bob. I was just mee'in around with you.

Moonbathing I'll put something together for you.