Help with reading XML [modified]
-
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, 2007The Devil Lives in all of us, It's up to you to let HIM/HER out!!!!!
-
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, 2007The Devil Lives in all of us, It's up to you to let HIM/HER out!!!!!
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.
-
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.
-
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!!!!!
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.
-
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.
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, 2007The Devil Lives in all of us, It's up to you to let HIM/HER out!!!!!