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. Help with reading XML [modified]

Help with reading XML [modified]

Scheduled Pinned Locked Moved C#
xmlhelptutorial
5 Posts 2 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.
  • V Offline
    V Offline
    velkropie
    wrote on last edited by
    #1

    Hello, i would like to have a windows form combo box populated from an xml file, here is the format of the xml file //i had to add quotes to the tags to display, there is no quotes in real code..thanks "<"ITEMS">" "<"NAME">""<"/"NAME">" "<"URL">""<"/"URL>" "<"/"ITEMS">" i want to take these two items and assign them as variables NewName; NewUrl; which i will then populate the combo box with... and read them with this code // Declare a variable of type XmlTextReader //XmlTextReader xtr = null; //// Declare a string that holds the name of the file //string fileName = "ConboItems.xml"; //try //{ // // Initialize the XmlTextReader variable with the name of the file // xtr = new XmlTextReader(fileName); // xtr.WhitespaceHandling = WhitespaceHandling.None; // // Scan the XML file // while (xtr.Read()) // { // // every time you find an element, find out what type it is // switch (xtr.NodeType) // { // case XmlNodeType.Text: // // If you find text, put it in the combo box' list // // this.comboChannels.Items.Add(xtr.Value); // break; // } // } // // For this example, select the first item // this.comboChannels.SelectedIndex = 0; //} //finally //{ // // Close the XmlTextReader // if (xtr != null) // xtr.Close(); //} -- modified at 15:46 Monday 9th July, 2007

    The Devil Lives in all of us, It's up to you to let HIM/HER out!!!!!

    V 1 Reply Last reply
    0
    • V velkropie

      Hello, i would like to have a windows form combo box populated from an xml file, here is the format of the xml file //i had to add quotes to the tags to display, there is no quotes in real code..thanks "<"ITEMS">" "<"NAME">""<"/"NAME">" "<"URL">""<"/"URL>" "<"/"ITEMS">" i want to take these two items and assign them as variables NewName; NewUrl; which i will then populate the combo box with... and read them with this code // Declare a variable of type XmlTextReader //XmlTextReader xtr = null; //// Declare a string that holds the name of the file //string fileName = "ConboItems.xml"; //try //{ // // Initialize the XmlTextReader variable with the name of the file // xtr = new XmlTextReader(fileName); // xtr.WhitespaceHandling = WhitespaceHandling.None; // // Scan the XML file // while (xtr.Read()) // { // // every time you find an element, find out what type it is // switch (xtr.NodeType) // { // case XmlNodeType.Text: // // If you find text, put it in the combo box' list // // this.comboChannels.Items.Add(xtr.Value); // break; // } // } // // For this example, select the first item // this.comboChannels.SelectedIndex = 0; //} //finally //{ // // Close the XmlTextReader // if (xtr != null) // xtr.Close(); //} -- modified at 15:46 Monday 9th July, 2007

      The Devil Lives in all of us, It's up to you to let HIM/HER out!!!!!

      V Offline
      V Offline
      Vikram A Punathambekar
      wrote on last edited by
      #2

      Not sure what you want, but does this help?

      XmlDocument doc = new XmlDocument();
      doc.Load("XMLFile1.xml");

      XmlNodeList names = doc.GetElementsByTagName("NAME");
      XmlNodeList urls = doc.GetElementsByTagName("URL");

      foreach (XmlNode node in names)
      {
      Console.WriteLine(node.InnerText);
      }

      foreach (XmlNode node in urls)
      {
      Console.WriteLine(node.InnerText);
      }

      Tip: Check the Ignore HTML... option if you're posting XML snippets.

      Cheers, Vıkram.


      After all is said and done, much is said and little is done.

      V 1 Reply Last reply
      0
      • V Vikram A Punathambekar

        Not sure what you want, but does this help?

        XmlDocument doc = new XmlDocument();
        doc.Load("XMLFile1.xml");

        XmlNodeList names = doc.GetElementsByTagName("NAME");
        XmlNodeList urls = doc.GetElementsByTagName("URL");

        foreach (XmlNode node in names)
        {
        Console.WriteLine(node.InnerText);
        }

        foreach (XmlNode node in urls)
        {
        Console.WriteLine(node.InnerText);
        }

        Tip: Check the Ignore HTML... option if you're posting XML snippets.

        Cheers, Vıkram.


        After all is said and done, much is said and little is done.

        V Offline
        V Offline
        velkropie
        wrote on last edited by
        #3

        hey, thanks for your response, how can i load the list of names, urls to a combobox based on your code? thanks again

        The Devil Lives in all of us, It's up to you to let HIM/HER out!!!!!

        V 1 Reply Last reply
        0
        • V velkropie

          hey, thanks for your response, how can i load the list of names, urls to a combobox based on your code? thanks again

          The Devil Lives in all of us, It's up to you to let HIM/HER out!!!!!

          V Offline
          V Offline
          Vikram A Punathambekar
          wrote on last edited by
          #4

          Mate, you already know how to add items to a combobox - you're doing it in your code. Just replace my Console.WriteLine() statements with your code.

          Cheers, Vıkram.


          After all is said and done, much is said and little is done.

          V 1 Reply Last reply
          0
          • V Vikram A Punathambekar

            Mate, you already know how to add items to a combobox - you're doing it in your code. Just replace my Console.WriteLine() statements with your code.

            Cheers, Vıkram.


            After all is said and done, much is said and little is done.

            V Offline
            V Offline
            velkropie
            wrote on last edited by
            #5

            ok, i'm using this, but the code is only loading one node to my combobox, can someone help me with this? XmlDocument doc = new XmlDocument(); doc.Load("ComboItemName.xml"); XmlNodeList Nodes = doc.GetElementsByTagName("NAME"); foreach (XmlNode node in Nodes) { NewNameLoad = node.InnerText.ToString(); } XmlNodeList NodesA = doc.GetElementsByTagName("URL"); foreach (XmlNode node in NodesA) { NewUrlLoad = node.InnerText.ToString(); } comboChannels.Items.Add(new RSSChannel(NewNameLoad, NewUrlLoad)); // And select the first one comboChannels.SelectedIndex = 0; -- modified at 13:49 Wednesday 11th July, 2007

            The Devil Lives in all of us, It's up to you to let HIM/HER out!!!!!

            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