tags:

views:

1020

answers:

2

Hello guys,

I am trying to get this code work for about 2 hours =( I am new to C# and don't know all the .NET library classes.

The target is to populate XML data to comboBox

DataSet dataSet = new DataSet();
DataTable dataTable = new DataTable("table1");
dataTable.Columns.Add("col1", typeof(string));
dataSet.Tables.Add(dataTable);

StringReader strR = new StringReader("<root><parm1>val1</parm1><parm2>val2</parm2></root>");

dataSet.ReadXml(strR);

comboBox1.DataSource = dataSet.Tables[0];
comboBox1.DisplayMember = "col1";
comboBox1.ValueMember = "col1";

Well, it doesn't work as expected. The ComboBox should show val1 val2

I don't really understand how column names of DataTable in a DataSet are related to XML-Tags... Maybe that's the point?

Thank You in advance!

+3  A: 

The following should work:

DataSet dataSet = new DataSet();
DataTable dataTable = new DataTable("table1");
dataTable.Columns.Add("col1", typeof(string));
dataSet.Tables.Add(dataTable);

StringReader strR = new StringReader("<root><table1><col1>val1</col1></table1><table1><col1>val2</col1></table1></root>");

dataSet.ReadXml(strR);

comboBox1.DataSource = dataSet.Tables[0];
comboBox1.DisplayMember = "col1";
comboBox1.ValueMember = "col1";

The names of the tables and the columns need to be consistent between your C# objects and XML data.

pmarflee
Thank You! This works.Thats really simple. The table is the parent node and the deepest nodes are the columns...
samunai
A: 

Thank you from me too. I was having the same issue and I was very happy to find a forum about it already.

-TJ

TJ