Solved

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

Posted on 2004-10-30
373 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
Question by:Moonbathing
    8 Comments
     

    Author Comment

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

    Assisted Solution

    by:Bob Learned
    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:
    Put your xml doc into a dataset sort the dataset and re-populate your xml.

    Tom
    0
     
    LVL 96

    Expert Comment

    by:Bob Learned
    Ooh, another great suggestion :)

    Bob
    0
     
    LVL 5

    Expert Comment

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

    I hopes your not being sarcastic Bob...
    0
     

    Author Comment

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

    Expert Comment

    by:Bob Learned
    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
    I know you were Bob. I was just mee'in around with you.

    Moonbathing I'll put something together for you.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    856 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now