[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 406
  • Last Modified:

**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 >>
0
Moonbathing
Asked:
Moonbathing
  • 3
  • 3
  • 2
2 Solutions
 
MoonbathingAuthor Commented:
Can anyone help me do a simple sort?
0
 
Bob LearnedCommented:
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
 
thenrichCommented:
Put your xml doc into a dataset sort the dataset and re-populate your xml.

Tom
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Bob LearnedCommented:
Ooh, another great suggestion :)

Bob
0
 
thenrichCommented:
'Ooh, another great suggestion :)'

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

Moonbathing I'll put something together for you.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now