Add sets of nodes to a text file in alphabetical order.

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 1404 Views - Last Post: 27 January 2017 - 12:48 PM Rate Topic: -----

#1 Dreamfall   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 137
  • Joined: 26-January 15

Add sets of nodes to a text file in alphabetical order.

Posted 23 January 2017 - 02:12 AM

Hi guys I need to enter a series of nodes within a text file in alphabetical order of the parent node.
In the example I enter any of the names to create the structure, paul, joseph, Anthony, Francis, Lewis, Helen, etc .. and I find myself in this situation.... :online2long:/>

<?xml version="1.0" encoding="utf-8"?>
<Setting>
  <paul>
    <node1>empty</node1>
    <node2>empty</node2>
    <node3>empty</node3>
    <node4>empty</node4>
    <node5>empty</node5>
    <node6>empty</node6>
  </paul>
  <joseph>
    <node1>empty</node1>
    <node2>empty</node2>
    <node3>empty</node3>
    <node4>empty</node4>
    <node5>empty</node5>
    <node6>empty</node6>
  </joseph>
  <Anthony>
    <node1>empty</node1>
    <node2>empty</node2>
    <node3>empty</node3>
    <node4>empty</node4>
    <node5>empty</node5>
    <node6>empty</node6>
  </Anthony>
  <Francis>
    <node1>empty</node1>
    <node2>empty</node2>
    <node3>empty</node3>
    <node4>empty</node4>
    <node5>empty</node5>
    <node6>empty</node6>
  </Francis>
  <Lewis_PACKED>
    <node1>empty</node1>
    <node2>empty</node2>
    <node3>empty</node3>
    <node4>empty</node4>
    <node5>empty</node5>
    <node6>empty</node6>
  </Lewis>
  <Helen>
    <node1>empty</node1>
    <node2>empty</node2>
    <node3>empty</node3>
    <node4>empty</node4>
    <node5>empty</node5>
    <node6>empty</node6>
  </Helen>
</Setting>



I need that when I insert any name or word of the parent node for create a new structure to those existing ones should insert within the text file in alphabetical order...in this way :smile2:/>

<Setting>
  <Anthony>
    <node1>empty</node1>
    <node2>empty</node2>
    <node3>empty</node3>
    <node4>empty</node4>
    <node5>empty</node5>
    <node6>empty</node6>
  </Anthony>
  <Francis>
    <node1>empty</node1>
    <node2>empty</node2>
    <node3>empty</node3>
    <node4>empty</node4>
    <node5>empty</node5>
    <node6>empty</node6>
  </Francis>
  <Helen>
    <node1>empty</node1>
    <node2>empty</node2>
    <node3>empty</node3>
    <node4>empty</node4>
    <node5>empty</node5>
    <node6>empty</node6>
  </Helen>
  <joseph>
    <node1>empty</node1>
    <node2>empty</node2>
    <node3>empty</node3>
    <node4>empty</node4>
    <node5>empty</node5>
    <node6>empty</node6>
  </joseph>
  <Lewis>
    <node1>empty</node1>
    <node2>empty</node2>
    <node3>empty</node3>
    <node4>empty</node4>
    <node5>empty</node5>
    <node6>empty</node6>
  </Lewis>
  <paul>
    <node1>empty</node1>
    <node2>empty</node2>
    <node3>empty</node3>
    <node4>empty</node4>
    <node5>empty</node5>
    <node6>empty</node6>
  </paul>
</Setting>



:helpsmilie:/> :dozingoff:/>

Is This A Good Question/Topic? 0
  • +

Replies To: Add sets of nodes to a text file in alphabetical order.

#2 andrewsw   User is online

  • blow up my boots
  • member icon

Reputation: 6549
  • View blog
  • Posts: 26,552
  • Joined: 12-December 12

Re: Add sets of nodes to a text file in alphabetical order.

Posted 23 January 2017 - 06:25 AM

Have you already written code to insert nodes?

Do you have control over the XML? That isn't a great format. Preferably the name should be an attribute to a,for example, name element.

You are not parsing as text are you? It's an XML file.

Rather than considering that you would insert the nodes in order, consider that you need to sort the nodes.
Was This Post Helpful? 0
  • +
  • -

#3 Dreamfall   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 137
  • Joined: 26-January 15

Re: Add sets of nodes to a text file in alphabetical order.

Posted 23 January 2017 - 09:05 AM

Quote

Have you already written code to insert nodes?

Yes..I've already written the code to insert nodes.

Quote

Do you have control over the XML? That isn't a great format. Preferably the name should be an attribute to a,for example, name element.

?

Quote

You are not parsing as text are you? It's an XML file.

if I understand the question, I save the file in *.txt for test, but I could save it in any format to read *.xml, *.dat or other.

Quote

Rather than considering that you would insert the nodes in order, consider that you need to sort the nodes.

The read and write code of the nodes in a file works perfectly, the only help I need is alphabetize the element name that I go to insert, as in the first example I posted.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • blow up my boots
  • member icon

Reputation: 6549
  • View blog
  • Posts: 26,552
  • Joined: 12-December 12

Re: Add sets of nodes to a text file in alphabetical order.

Posted 23 January 2017 - 10:25 AM

I have no idea how you are inserting a node but presumably you could iterate the nodes to insert it at the correct location. That is, iterate until you find the value that is higher alphabetically.
Was This Post Helpful? 0
  • +
  • -

#5 Dreamfall   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 137
  • Joined: 26-January 15

Re: Add sets of nodes to a text file in alphabetical order.

Posted 23 January 2017 - 10:43 AM

do you place your portion for inserting nodes...

Dim User_Config As New XmlDocument()
    Private Sub Add_Config()
        Dim ADD As XmlElement = User_Config.CreateElement(TextBox1.Text)
        Dim CTRL_1 As XmlElement = User_Config.CreateElement("Node1")
        Dim CTRL_2 As XmlElement = User_Config.CreateElement("Node2")
        Dim CTRL_3 As XmlElement = User_Config.CreateElement("Node3")
        Dim CTRL_4 As XmlElement = User_Config.CreateElement("Node4")
        Dim CTRL_5 As XmlElement = User_Config.CreateElement("Node5")
        Dim CTRL_6 As XmlElement = User_Config.CreateElement("Node6")
        CTRL_1.InnerText = TextBox2.Text : ADD.AppendChild(CTRL_1)
        CTRL_2.InnerText = TextBox3.Text : ADD.AppendChild(CTRL_2)
        CTRL_3.InnerText = TextBox4.Text : ADD.AppendChild(CTRL_3)
        CTRL_4.InnerText = TextBox5.Text : ADD.AppendChild(CTRL_4)
        CTRL_5.InnerText = TextBox6.Text : ADD.AppendChild(CTRL_5)
        CTRL_6.InnerText = TextBox7.Text : ADD.AppendChild(CTRL_6)
        User_Config.DocumentElement.AppendChild(ADD)
        User_Config.Save(Application.StartupPath & "\" & "Test.txt")
    End Sub



User_Config.CreateElement(TextBox1.Text)[b] 'Here we have to alphabetize[/b]



I hope for a solution.... :rolleyes:/>/>/>

This post has been edited by Dreamfall: 23 January 2017 - 10:46 AM

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14088
  • View blog
  • Posts: 56,440
  • Joined: 12-June 08

Re: Add sets of nodes to a text file in alphabetical order.

Posted 23 January 2017 - 10:52 AM

Quote

I hope for a solution.... :rolleyes:/>/>/>


Friendly Monday reminder, please be careful about borderline asking people to do your work for you.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is online

  • blow up my boots
  • member icon

Reputation: 6549
  • View blog
  • Posts: 26,552
  • Joined: 12-December 12

Re: Add sets of nodes to a text file in alphabetical order.

Posted 23 January 2017 - 11:11 AM

There is an InsertBefore and an InsertAfter method. You could, as mentioned, iterate the nodes and pause when you find the right spot.
Was This Post Helpful? 1
  • +
  • -

#8 Dreamfall   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 137
  • Joined: 26-January 15

Re: Add sets of nodes to a text file in alphabetical order.

Posted 23 January 2017 - 01:05 PM

:eek:/>

Quote

Friendly Monday reminder, please be careful about borderline asking people to do your work for you.

I have not asked for to be done a whole project or work, but only the possibility of alphabetize the nodes in a text files when they are inserted...maybe it's me that I expressed myself badly.

Quote

There is an InsertBefore and an InsertAfter method. You could, as mentioned, iterate the nodes and pause when you find the right spot.

Inserts the specified node immediately before the specified reference node, but not in alphabetical order.. :dozingoff:/>
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is online

  • blow up my boots
  • member icon

Reputation: 6549
  • View blog
  • Posts: 26,552
  • Joined: 12-December 12

Re: Add sets of nodes to a text file in alphabetical order.

Posted 23 January 2017 - 01:20 PM

Quote

Inserts the specified node immediately before the specified reference node, but not in alphabetical order..

If the existing nodes are already in order then you would march through them until you find the correct location for the new node and insert it.

If the existing nodes are not already in order then, as I mentioned earlier, you would need to sort them first. (I was also hinting that you could just append the node and sort the nodelist on each occasion, to ensure that the list remains sorted.)



Quote

I have not asked for to be done a whole project or work, but only the possibility of alphabetize the nodes in a text files when they are inserted...

The amount of code is immaterial, it is a request for code to be written that is not permitted.
Was This Post Helpful? 0
  • +
  • -

#10 Dreamfall   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 137
  • Joined: 26-January 15

Re: Add sets of nodes to a text file in alphabetical order.

Posted 23 January 2017 - 01:38 PM

Quote

(I was also hinting that you could just append the node and sort the nodelist on each occasion, to ensure that the list remains sorted.)
YES

I tried but did not succeed... :sleepy:
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw   User is online

  • blow up my boots
  • member icon

Reputation: 6549
  • View blog
  • Posts: 26,552
  • Joined: 12-December 12

Re: Add sets of nodes to a text file in alphabetical order.

Posted 23 January 2017 - 01:59 PM

What did you try? Post your attempt.
Was This Post Helpful? 0
  • +
  • -

#12 Dreamfall   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 137
  • Joined: 26-January 15

Re: Add sets of nodes to a text file in alphabetical order.

Posted 23 January 2017 - 03:29 PM

Tomorrow post the solution to the problem, now my head is that I goes up in smoke...... :death: ;) :D
Was This Post Helpful? 0
  • +
  • -

#13 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1498
  • View blog
  • Posts: 3,796
  • Joined: 01-February 13

Re: Add sets of nodes to a text file in alphabetical order.

Posted 23 January 2017 - 07:20 PM

I would suggest looking into using the XDocument Class and the XElement Class to create your xml file and add elements to it. After reading andrewsw`s tutorial Query XML with XElement and LINQ to XML, it opened my eyes up to using these classes rather than the XmlDocument and XmlElement classes.

Just for the heck of it, using the XDocument and XElement classes, i wrote a small test app just to try keeping the elements in alphabetical order and found that it can be done with about the same amount or just a little less code than you have in your Add_Config sub already.

Anyways, is there any real reason that you need to keep the elements in alphabetical order in the file?

If you just want to do that so they are read from the file in alphabetical order for some reason, you could just read all the elements no mater what order they are in and use the OrderBy() method on the Elements to sort them in alphabetical order before listing them or doing whatever you need to with them. That is covered in andrewsw`s tutorial.
Was This Post Helpful? 1
  • +
  • -

#14 Dreamfall   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 137
  • Joined: 26-January 15

Re: Add sets of nodes to a text file in alphabetical order.

Posted 24 January 2017 - 12:26 AM

Hi Maestro, it is always a pleasure to have you in my open thread.

Quote

Anyways, is there any real reason that you need to keep the elements in alphabetical order in the file?


I thirst to learn and hungry to know, I have to practice for a few studies, and I have to get to a good level of programming, or nearly so. :D/>/>

I examined XDocument Class and the XElement Class , but I chose the method OrderBy(). :smile2:/>/>

This post has been edited by Dreamfall: 24 January 2017 - 12:28 AM

Was This Post Helpful? 0
  • +
  • -

#15 andrewsw   User is online

  • blow up my boots
  • member icon

Reputation: 6549
  • View blog
  • Posts: 26,552
  • Joined: 12-December 12

Re: Add sets of nodes to a text file in alphabetical order.

Posted 25 January 2017 - 04:46 PM

@Dreamfall Will you be posting your solution?

@IronRazer I also built a small sample which follows, I'd be interested to compare them ;) (as I might consider posting a Snippet).

        Dim doc As XDocument = Xdocument.Load("c:\users\andrew\documents\test.xml")
        Dim name As String = "Dave"

        Dim bloke = <<%= name %>>
                        <node1>empty</node1>
                        <node2>empty</node2>
                        <node3>empty</node3>
                        <node4>empty</node4>
                        <node5>empty</node5>
                        <node6>empty</node6>
                    </>

        doc.Root.Add(bloke)

        Dim elems = From el In doc.Root.Elements
                    Order By el.Name.LocalName
                    Select el

        For Each el In elems
            Console.WriteLine(el)
        Next

        doc.Root.ReplaceAll(elems)
        doc.Save("c:\users\andrew\documents\test2.xml")

Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2