Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. load combobox from xml [modified]

load combobox from xml [modified]

Scheduled Pinned Locked Moved C#
databasexml
12 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C CPallini

    Well, as you guessed, posting relevant code will be helpful. BTW your XML file doesn't seem very elegant, for instance, in: <Days>21,"21"</Days> you enclose two pieces of info inside a single item. Maybe it is not a good idea. :)

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

    P Offline
    P Offline
    Planker
    wrote on last edited by
    #3

    xtr = new XmlTextReader(filename); while (xtr.Read()) { if (xtr.NodeType == XmlNodeType.Element) { if (xtr.LocalName.Equals("Days")) { this.cbodays.Items.Add(xtr.Value); } } } the 21,"21" was just test data, I was trying to use the 20 but that gave me just the blank drop down box, so i was wondering if it just took my numbers and made them the index of the combobox so I put a few different data formats in the XML file to see if that made any difference I have used a switch statement to check for XmlNodeType.Element and gives same results, if I change .equals("days") to an name that is not in the file I just get one blank drop down, currently it looks like it see that there are the right number of records but no data in the drop down Rob

    C 1 Reply Last reply
    0
    • P Planker

      xtr = new XmlTextReader(filename); while (xtr.Read()) { if (xtr.NodeType == XmlNodeType.Element) { if (xtr.LocalName.Equals("Days")) { this.cbodays.Items.Add(xtr.Value); } } } the 21,"21" was just test data, I was trying to use the 20 but that gave me just the blank drop down box, so i was wondering if it just took my numbers and made them the index of the combobox so I put a few different data formats in the XML file to see if that made any difference I have used a switch statement to check for XmlNodeType.Element and gives same results, if I change .equals("days") to an name that is not in the file I just get one blank drop down, currently it looks like it see that there are the right number of records but no data in the drop down Rob

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #4

      Planker wrote:

      this.cbodays.Items.Add(xtr.Value);

      Substitute xtr.Value with xtr.ReadElementString() in the above line. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

      P 1 Reply Last reply
      0
      • C CPallini

        Planker wrote:

        this.cbodays.Items.Add(xtr.Value);

        Substitute xtr.Value with xtr.ReadElementString() in the above line. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        P Offline
        P Offline
        Planker
        wrote on last edited by
        #5

        Thank you, that worked. is there a reason that xtr.Value works for text boxes and you need readelementstring for combobox?

        C 1 Reply Last reply
        0
        • P Planker

          Thank you, that worked. is there a reason that xtr.Value works for text boxes and you need readelementstring for combobox?

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #6

          No, there isn't. I think that xtr.Value doesn't work for text boxes too. But I must test it...tomorrow...maybe!

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

          P 1 Reply Last reply
          0
          • C CPallini

            No, there isn't. I think that xtr.Value doesn't work for text boxes too. But I must test it...tomorrow...maybe!

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

            P Offline
            P Offline
            Planker
            wrote on last edited by
            #7

            thank you for your help, wondering if you could answer a question about writing back to the xml file. I think you are right, I looked at my other code again and i was using ReadString I am getting an error at run time on this line of code newDayIndex.InnerXml = index.ToString(); here is how i'm doing all the xml code XmlTextReader reader = new XmlTextReader(C_strFileName); XmlDocument doc = new XmlDocument(); doc.Load(reader); reader.Close(); XmlNode oldIndex; XmlElement root = doc.DocumentElement; oldIndex = root.SelectSingleNode("/config/Days_Index"); XmlElement newDayIndex = doc.CreateElement("Days_Index"); newDayIndex.InnerXml = index.ToString(); root.ReplaceChild(newDayIndex, oldIndex); doc.Save(C_strFileName);

            1 Reply Last reply
            0
            • P Planker

              I am trying to load a list of number in a combobox, it seems to be reading the xml file elements but it is reading my numbers as the index for the combobox here is my xml file data that is is reading 21, "21" 20, "20" 19 19 18 17 16 15 14 13 12 11 10 all i get is a blank drop down list, if you want to see the code let me know but the code is working to read the other elements and filling text boxes -- modified at 9:53 Saturday 5th May, 2007

              A Offline
              A Offline
              AFSEKI
              wrote on last edited by
              #8
              1. You should first correct these: 21, "21" 20, "20" 2) Then check if it you are really reading the values from the XML file correctly. 3) Use Convert.ChangeType(...) to convert the values you read to a specific type if you want. 4) Update ComboBox.DataSource Property with the list of values you read ( check out MSDN Library for ComboBox.DataSource Property on how to add specific, custom data sources to ComboBox ) Hope this helps...
              P 1 Reply Last reply
              0
              • A AFSEKI
                1. You should first correct these: 21, "21" 20, "20" 2) Then check if it you are really reading the values from the XML file correctly. 3) Use Convert.ChangeType(...) to convert the values you read to a specific type if you want. 4) Update ComboBox.DataSource Property with the list of values you read ( check out MSDN Library for ComboBox.DataSource Property on how to add specific, custom data sources to ComboBox ) Hope this helps...
                P Offline
                P Offline
                Planker
                wrote on last edited by
                #9

                Thanks for the info, i got the loading of the combobox working, i am not trying to figure out how to save the changes back to the xml file if the user changes the value.

                A 1 Reply Last reply
                0
                • P Planker

                  Thanks for the info, i got the loading of the combobox working, i am not trying to figure out how to save the changes back to the xml file if the user changes the value.

                  A Offline
                  A Offline
                  AFSEKI
                  wrote on last edited by
                  #10

                  I know that you are not trying to save back the user modified values to the XML file. What I suggest using Convert is to give you the ability to store the datatype of the object(what ever you create) as a string value and then convert it to what you want. You may also write a custom type converter or implement iSerializable interface or add XmlAttributes to your fields for which you want to serialize.

                  P 1 Reply Last reply
                  0
                  • A AFSEKI

                    I know that you are not trying to save back the user modified values to the XML file. What I suggest using Convert is to give you the ability to store the datatype of the object(what ever you create) as a string value and then convert it to what you want. You may also write a custom type converter or implement iSerializable interface or add XmlAttributes to your fields for which you want to serialize.

                    P Offline
                    P Offline
                    Planker
                    wrote on last edited by
                    #11

                    yes I am trying to save back the user modified values. I'm updating a config file so when the program is shutdown the changed value is read the next time the program is run.

                    A 1 Reply Last reply
                    0
                    • P Planker

                      yes I am trying to save back the user modified values. I'm updating a config file so when the program is shutdown the changed value is read the next time the program is run.

                      A Offline
                      A Offline
                      AFSEKI
                      wrote on last edited by
                      #12

                      Do you know what you want? This is your post: Thanks for the info, i got the loading of the combobox working, i am not trying to figure out how to save the changes back to the xml file if the user changes the value. And now you are saying: yes I am trying to save back the user modified values. I'm updating a config file so when the program is shutdown the changed value is read the next time the program is run. Are you OK?

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups