Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading XML files and load specify data into combo box

Status
Not open for further replies.

edizone

Programmer
Oct 3, 2006
2
SE
Hi all,

This is my first post in Tek-Tips forum, hope can get some helps and guides from you all..:)
My question sound like this:

I'm currently develop a window base applicationin c#. So far, I had create a xml files and plan to load the data into combo box accordingly base on the node name, example:

a) Sample of XML files:
**********************************************
<Material>
<C>Ceramic</C>
<L>Glass</L>
</Material>
<Position>
<A>Axial</A>
<B>Bottom</B>
<D>Dual</D>
</Position>
***********************************************

So, there will be two different combo box, one to store 'Material' and the other one to store 'Position', i want the data such as Ceramic, glass load into first combo box, and data such as "Axial", "Bottom" into second combo box,
how can i do that.

I used XML TextReader but seem like i can't specify which node to load, the code will give me the whole things(ceramic, glass, axial, bottom, dual), here is the code i used:

*************************************************
xtr = new XmlTextReader(fileName);
xtr.WhitespaceHandling = WhitespaceHandling.None;

while (xtr.Read())
{
switch (xtr.NodeType)
{
case XmlNodeType.Text: this.cboMaterial.Items.Add(xtr.Value); break;

}
}
***************************************************


Any help is appreciated, please help and thanks..
 
You also need to check for the <Material> and <Position> tags, which will tell you which combo box to load.
 
Hi thanks for your reply Miros..
Ya i know, but XmlTextReader class seem like cant validate xml data base on the tag name, i meant how could i know which tag to read on?
 
Try
Points it raises:
To perform data validation, use a validating XmlReader. For more information, see Validating XML Data with XmlReader.

To read XML data from an XmlDocument, use XmlNodeReader.

Also check . I think you just need to add another case to your switch:
Code:
HWND combobox = NULL ;
switch (xtr.NodeType)
{
    case XmlNodeType.Text:
        if ( combobox != NULL )
            combobox.Items.Add(xtr.Value);
        break;
    case XmlNodeType.Element: 
        if ( strcmp( xtr.Name, "Material" ) == 0 ) 
            combobox = this.cboMaterial ; 
        else if ( strcmp( xtr.Name, "Position" ) == 0 ) 
            combobox = this.cboPosition ;
        break ;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top