Inserting data into XML - alphabetized?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1476 Views - Last Post: 16 August 2015 - 06:45 AM Rate Topic: -----

#1 Recoil  Icon User is offline

  • D.I.C Addict

Reputation: 51
  • View blog
  • Posts: 504
  • Joined: 28-June 08

Inserting data into XML - alphabetized?

Posted 15 August 2015 - 05:01 PM

I know how to enter data into an xml file. I have been scouring for the last day or so trying to find a method of some sort to insert data in a specific location, such as an alphabetized list. So far I have not found anything. Here is how my xml file looks so far:
<Manufacturers>
  <Manufacturer
    Name="Misc">
    <Flavoring
      Name="Water"
      Base="Misc"
      Weight="1.00">
    </Flavoring>
    <Flavoring
      Name="Vodka"
      Base="Misc"
      Weight="1.00">
    </Flavoring>
    <Flavoring
      Name="Saline"
      Base="Misc"
      Weight="1.00">
    </Flavoring>
    <Flavoring
      Name="Apple Cider Vinegar"
      Base="Misc"
      Weight="1.00">
    </Flavoring>
  </Manufacturer>
</Manufacturers>



When I insert a new manufacturer, or a flavoring for that manufacturer, I need these to insert alphabetized instead of like above. I'm honestly lost on a way to approach this and am looking for an idea a bit more explanatory than "use insert before". I already have a combobox setup listing the mftg's and a listbox for the flavorings displaying those only for the selected mftg.

I really would appreciate an idea of how to achieve something like this. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Inserting data into XML - alphabetized?

#2 andrewsw  Icon User is offline

  • I'm a doctor and want my sausages
  • member icon

Reputation: 6323
  • View blog
  • Posts: 25,447
  • Joined: 12-December 12

Re: Inserting data into XML - alphabetized?

Posted 15 August 2015 - 05:23 PM

Post the code that you are currently using, particularly as there are several approaches to working with XML.

Quote

Here is how my xml file looks so far:

Also confirm if you are creating this file, or first reading an existing file before modifying (over-writing) it.

I will also mention that, although it is possible to write the file in sorted order, this order should not be assumed when reading it back.

This post has been edited by andrewsw: 15 August 2015 - 05:26 PM

Was This Post Helpful? 0
  • +
  • -

#3 Recoil  Icon User is offline

  • D.I.C Addict

Reputation: 51
  • View blog
  • Posts: 504
  • Joined: 28-June 08

Re: Inserting data into XML - alphabetized?

Posted 15 August 2015 - 05:37 PM

Thanks for replying. As far as how the xml file is created I did this through the designer, setup how I am accessing the nodes (mftg, flavoring). I am using the existing file that is stored in the resources.


Right now I am only accessing the xml file to populate a combo box with the manufacturers, and a list box with the selected mftg flavorings:
    Dim _xmlCategories As XDocument
    Dim _xmlFlavorings As XDocument

    Private Sub FlavoringEditor_Load(sender As Object, e As EventArgs) Handles Me.Load

        ' Define this here to prevent FileNotFound errors.
        _xmlCategories = Xdocument.Load(Application.StartupPath & "\Data\" & "Categories.xml")
        _xmlFlavorings = Xdocument.Load(Application.StartupPath & "\Data\" & "Flavorings.xml")

        ' reset the mftg's and flavorings
        ResetControls()

    End Sub

    Private Sub ResetManufacturers()

        cbxFlavorManufacturer.Items.Clear()
        
        For Each item As XElement In _xmlFlavorings.Descendants("Manufacturer")

            cbxFlavorManufacturer.Items.Add(item.Attribute("Name").Value)

        Next
        
    End Sub

    Private Sub ResetFlavorings()

        lstMftgFlavors.Items.Clear()

        For Each item As XElement In _xmlFlavorings.Descendants("Flavoring")

            If item.Parent.Attribute("Name") = cbxFlavorManufacturer.Text Then
                lstMftgFlavors.Items.Add(item.Attribute("Name").Value)
            End If

        Next

    End Sub




I have already deleted my code that I was using to insert new nodes with a button click event because it was not being inserted alphabetically, only at then end of the document. If it will help I can recreate it.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • I'm a doctor and want my sausages
  • member icon

Reputation: 6323
  • View blog
  • Posts: 25,447
  • Joined: 12-December 12

Re: Inserting data into XML - alphabetized?

Posted 15 August 2015 - 06:03 PM

In my view it remains a little unclear so it would help if you re-produced something similar to what you were attempting. (Are you writing according to what's listed, and added, to the combobox and listbox?)

You can sort using XElement:

How to: Sort Elements

You can sort a combobox and listbox:

ComboBox.Sorted Property
(this also means that items will be added in sorted order)

ListBox.Sort Method (also has Sorted property)
Was This Post Helpful? 1
  • +
  • -

#5 Recoil  Icon User is offline

  • D.I.C Addict

Reputation: 51
  • View blog
  • Posts: 504
  • Joined: 28-June 08

Re: Inserting data into XML - alphabetized?

Posted 15 August 2015 - 06:16 PM

Sorry, I had not even thought about just appending the additional mftg's and flavorings then just sorting the combo and list boxes.

Is that typically how something like this is handled?
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • I'm a doctor and want my sausages
  • member icon

Reputation: 6323
  • View blog
  • Posts: 25,447
  • Joined: 12-December 12

Re: Inserting data into XML - alphabetized?

Posted 15 August 2015 - 06:44 PM

I don't think that there is an approach that could be described as typical with XML, particularly without the full picture of what your goals are and your application does.

If your intention is for the ComboBox and ListBox content to be written out as XML, over-writing the original file or creating a new one, then, yes, you could set Sorted to True for both controls. As I mentioned, when new items are added they will be added in order.

It is possible to bind to XML, with the recommended approach being to Fill a DataSet and bind to that (or via a BindingSource). (I don't think that XML-binding is as well-formed a feature in WinForms as it is in WPF.)



What are you doing? Are you populating the ComboBox and ListBox from an XML file, allowing the user to add new items to these controls, then replacing the original XML file with a new version, constructed from the controls' items?

But how are the items in the CB associated with items in the LB?

This post has been edited by andrewsw: 15 August 2015 - 06:51 PM

Was This Post Helpful? 0
  • +
  • -

#7 Recoil  Icon User is offline

  • D.I.C Addict

Reputation: 51
  • View blog
  • Posts: 504
  • Joined: 28-June 08

Re: Inserting data into XML - alphabetized?

Posted 15 August 2015 - 07:03 PM

Thanks. Right now I am doing like your last message, populating the controls from the xml file with the mftg for the combobox, and the flavorings in the listbox for that mftg. From there I am going to allow the user to add a new mftg to the xml file from an input box, as well as allowing the user to change certain data for each of the flavorings and add new ones from additional controls on the form.

Here is my entire source so you might get a better idea of what I am trying to do with it. The flavoring editor can be accessed from the File menu on the calculator:
Attached File  VapeMachine.zip (1.43MB)
Number of downloads: 96

I'm fine with just editing the xml file itself, but I have had a couple of users request an easier method of adding/editing them. I'm honestly not sure of a better method of going about this. XML was the only thing I could think of without needing an entire database to house all this information. However it may be what I need in the long run, but an Access file is the only DB I would be able to work with from my experience.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • I'm a doctor and want my sausages
  • member icon

Reputation: 6323
  • View blog
  • Posts: 25,447
  • Joined: 12-December 12

Re: Inserting data into XML - alphabetized?

Posted 15 August 2015 - 07:11 PM

Oh, on looking at your code again I see that you are listing Flavorings for the current Manufacturer.
Was This Post Helpful? 0
  • +
  • -

#9 Recoil  Icon User is offline

  • D.I.C Addict

Reputation: 51
  • View blog
  • Posts: 504
  • Joined: 28-June 08

Re: Inserting data into XML - alphabetized?

Posted 15 August 2015 - 07:21 PM

Yes. Currently there are 7 major manufacturers that people use, with a few small manufacturers, for flavors used in vaping recipes. So far I have only done 2 of them in the xml. Due to the amount of flavors for each mftg I had to have some way to sort it so they aren't looking at 1000+ flavors, LOL

Once I recreate the adding a flavor and mftg button subs to insert new ones then I should pretty much be where I need to be so that I can open source this on GitHub.

I appreciate the help with pointing me to sort just the controls...that was so simple I was just over thinking how to go about it.
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is offline

  • I'm a doctor and want my sausages
  • member icon

Reputation: 6323
  • View blog
  • Posts: 25,447
  • Joined: 12-December 12

Re: Inserting data into XML - alphabetized?

Posted 15 August 2015 - 07:35 PM

So.. you're sorted then? Oh good ;)
Was This Post Helpful? 1
  • +
  • -

#11 andrewsw  Icon User is offline

  • I'm a doctor and want my sausages
  • member icon

Reputation: 6323
  • View blog
  • Posts: 25,447
  • Joined: 12-December 12

Re: Inserting data into XML - alphabetized?

Posted 15 August 2015 - 07:41 PM

If your data is as simple as shown then XML should be fine. If it is a shared resource though, or the structure will become more complicated (or the source needs to be stored securely, etc., etc.), then it should be in a database.

SQL Server, SQL Server Express, SQLite, SQL Server Compact Edition are some alternatives to Access, depending on whether you will be distributing the database or connecting centrally. The code you'll write will be pretty much the same anyway: connecting to the database, filling a DataSet, SQL statements.

This post has been edited by andrewsw: 15 August 2015 - 07:43 PM

Was This Post Helpful? 1
  • +
  • -

#12 Recoil  Icon User is offline

  • D.I.C Addict

Reputation: 51
  • View blog
  • Posts: 504
  • Joined: 28-June 08

Re: Inserting data into XML - alphabetized?

Posted 15 August 2015 - 07:47 PM

Sorry, yes I am all sorted out with this issue. Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

#13 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 117
  • View blog
  • Posts: 646
  • Joined: 01-October 08

Re: Inserting data into XML - alphabetized?

Posted 16 August 2015 - 04:18 AM

Before going much further it would be a good idea to consider a different XML design. In this, Principles of XML design: When to use elements versus attributes, you will find this, "If you consider the information in question to be part of the essential material that is being expressed or communicated in the XML, put it in an element." Given those guidelines maybe something like this would be better:

<Manufacturers>
  <Manufacturer>
    <mfgname>Mfg. One</mfgname>
    <flavorings>
      <flavor>
        <flavorname>Water</flavorname>
        <base>Misc.</base>
        <weight unit="lbs.">1.00</weight>
      </flavor>
      <flavor>
        <flavorname>Vodka</flavorname>
        <base>Misc.</base>
        <weight unit="ozs.">1.00</weight>
      </flavor>
    </flavorings>
  </Manufacturer>
</Manufacturers>


In the long run you might be happier with this structure. Note how I used an attribute for weight.

This post has been edited by dbasnett: 16 August 2015 - 04:19 AM

Was This Post Helpful? 0
  • +
  • -

#14 Recoil  Icon User is offline

  • D.I.C Addict

Reputation: 51
  • View blog
  • Posts: 504
  • Joined: 28-June 08

Re: Inserting data into XML - alphabetized?

Posted 16 August 2015 - 06:03 AM

I want to say I had actually asked about this before, and was told there really isn't a defined way to use either one. Normally if the weights were using multiple measurements (lbs, oz, g) I would have probably went that route. However, the calculator only calculates the gram weight of liquids due to the small amount of recipes that are created (either 10 or 30 ml).

I was also left with the impression that you use elements when there can be multiple elements, and attributes when there is only 1. But in a nutshell it is designed like it is because it is what I could get working, LOL.

When I initially began making this I was going to make this specifically using only flavorings for 1 manufacturer because that is all I use and this was solely just for personal use. It was requested to use other mftg's and their flavorings so I figured I would oblige so others could use this as well. I did not know the vast amount of flavorings that other mftg's had, so this is going to wind up turning into a really large xml file.

Even though as andrewsw has pointed out, my data should be fine in an xml file because this is all it is going to be...but it may be a better investment to move to a db file just due to the size.

Even though this is off topic: My goal was to make something that was offline. I wanted to distribute a complete xml file with the application for users to be able to edit if they wanted. Would a better approach be to centralize the db file online so when an item is entered from any user then the everyone will have access to the stuff?
Was This Post Helpful? 0
  • +
  • -

#15 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 117
  • View blog
  • Posts: 646
  • Joined: 01-October 08

Re: Inserting data into XML - alphabetized?

Posted 16 August 2015 - 06:32 AM

The attribute was to illustrate when attributes should be used. I think the quote about the usage of elements v. attributes is one of the best I have seen. If there are specific problems you have using properly structured XML you should ask.

Whether or not you use a centralized DB or distribute an XML file is up to you. If you have the resources to centralize the information is key; do you have a public presence on the web. How often is the data changed? How big is a "really large xml file"?

I have a database that has a table that has a column that is XML.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2