Feed CheckedListBox with data from XML
-
I am writting a small application that works with remote server via http. My application sends a request and in return receives an xml file (i can access it via a string var.) now I need to build a CheckedListBox with the data from this XML. Can someone suggest a simple way to arrange this and maybe give a sample? my XML file looks like a lot of small containers that look like this (for each item): 1 040104 I posted a subject here! content is posted here I want to be able to build CheckedListBox with 4 rows: id, post_date, subject, content. thanks a lot! Alexander
-
I am writting a small application that works with remote server via http. My application sends a request and in return receives an xml file (i can access it via a string var.) now I need to build a CheckedListBox with the data from this XML. Can someone suggest a simple way to arrange this and maybe give a sample? my XML file looks like a lot of small containers that look like this (for each item): 1 040104 I posted a subject here! content is posted here I want to be able to build CheckedListBox with 4 rows: id, post_date, subject, content. thanks a lot! Alexander
First of all, design your XML document to reflect alternatives which are more like siblings. Because you have a fragment like:
<items>
<item>
<id>1</id>
<post_date>2004-01-03T00:00:00</post_date>
<!-- and the rest -- >
</item>
<item>
<id>2</id>
<post_date>2004-01-03T01:00:00</post_date>
<!-- and the rest -- >
</item>
</items>This implies that each item should be a separate row. This isn't absolute, but common. If you design your document like this, you could actually use
DataSet.ReadXml
and create aDataSet
which is easily bindable to aCheckedListBox
. If you want to use your schema that you posted, you'll have to parse the XML document manually and create anIList
orIListSource
implementation. The easiest way would be to use anArrayList
, which implementsIList
. You can then use this object with theCheckedListBox.DataBindings
property (if memory serves me correctly), or enumerate the list and add each item to theCheckedListBox.Items
property. If you use the latter method, you can forego theArrayList
and just add the different XML elements to theCheckedListBox.Items
property in the first place. For information on multi-columnCheckedListBox
es, see theCheckedListBox.MultiColumn
property documentation, which inherits from theListBox
control class.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
First of all, design your XML document to reflect alternatives which are more like siblings. Because you have a fragment like:
<items>
<item>
<id>1</id>
<post_date>2004-01-03T00:00:00</post_date>
<!-- and the rest -- >
</item>
<item>
<id>2</id>
<post_date>2004-01-03T01:00:00</post_date>
<!-- and the rest -- >
</item>
</items>This implies that each item should be a separate row. This isn't absolute, but common. If you design your document like this, you could actually use
DataSet.ReadXml
and create aDataSet
which is easily bindable to aCheckedListBox
. If you want to use your schema that you posted, you'll have to parse the XML document manually and create anIList
orIListSource
implementation. The easiest way would be to use anArrayList
, which implementsIList
. You can then use this object with theCheckedListBox.DataBindings
property (if memory serves me correctly), or enumerate the list and add each item to theCheckedListBox.Items
property. If you use the latter method, you can forego theArrayList
and just add the different XML elements to theCheckedListBox.Items
property in the first place. For information on multi-columnCheckedListBox
es, see theCheckedListBox.MultiColumn
property documentation, which inherits from theListBox
control class.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
well, my XML file looks like what you have shown above: all are stored withing one so it looks like: I made a mistake, I want to show the data in a datagrid, not a checkedlistBox. The problem is that this XML is actually a string... I created a new dataset. how can I make dataset.ReadXml() read from a string? many-many thanks! Alexander.
-
well, my XML file looks like what you have shown above: all are stored withing one so it looks like: I made a mistake, I want to show the data in a datagrid, not a checkedlistBox. The problem is that this XML is actually a string... I created a new dataset. how can I make dataset.ReadXml() read from a string? many-many thanks! Alexander.
dophka wrote: I want to show the data in a datagrid, not a checkedlistBox. Good, a
DataGrid
is easier to data-bind since you only have to use theDataSource
property. You should also consider using theDataMember
andTableStyles
properties. See the SDK documentation for details or play around with the designer in VS.NET a little. It's not too difficult. dophka wrote: I created a new dataset. how can I make dataset.ReadXml() read from a string? You'll need to read the fragment as aTextReader
unfortunately. Let's say your XML fragment is in a string variable namedxml
:StringReader reader = null;
DataSet ds = null;
try
{
reader = new StringReader(xml);
ds = new DataSet("news");
ds.ReadXml(reader);
}
finally
{
if (reader != null) reader.Close();
}-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
dophka wrote: I want to show the data in a datagrid, not a checkedlistBox. Good, a
DataGrid
is easier to data-bind since you only have to use theDataSource
property. You should also consider using theDataMember
andTableStyles
properties. See the SDK documentation for details or play around with the designer in VS.NET a little. It's not too difficult. dophka wrote: I created a new dataset. how can I make dataset.ReadXml() read from a string? You'll need to read the fragment as aTextReader
unfortunately. Let's say your XML fragment is in a string variable namedxml
:StringReader reader = null;
DataSet ds = null;
try
{
reader = new StringReader(xml);
ds = new DataSet("news");
ds.ReadXml(reader);
}
finally
{
if (reader != null) reader.Close();
}-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
thanks a lot for your explanations! how I have done reading the xml thingy and I am sorry for asking, but then what? do I create another dataSet, add columns and then fill it with the data from the dataset that grabbed the xml content? can you please please please walk me little further? thanks! Alexander
-
thanks a lot for your explanations! how I have done reading the xml thingy and I am sorry for asking, but then what? do I create another dataSet, add columns and then fill it with the data from the dataset that grabbed the xml content? can you please please please walk me little further? thanks! Alexander
I'm the kind of guy that would rather teach a man to fish than give him a fish. So, read the documentation for the
DataGrid
. You already have yourDataSet
, so you assign that to theDataGrid.DataSource
property and set theDataGrid.DataMember
property to the table name you want to bind to. Columns are automatically created for each column of that table, but you can control this with theDataGrid.TableStyles
property. Try doing all this in VS.NET using the designer. Create a newDataSet
schema then use the Data toolbox to add a newDataSet
object. Add aDataGrid
to your form or control and play around with some of the properties. Reading the documentation will fill in the details, which are important to understand. There are also many great articles on data-binding here on CodeProject. Just use the search box toward the top and use generic terms like "DataSet" or "DataGrid". I'm sure you'll find plenty. Besides, going into any great deal really isn't meant for the forums. With all the possibilities, this is better discussed in an article. Always be sure to read the documentation, though. It should be your first stop for any questions.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
I'm the kind of guy that would rather teach a man to fish than give him a fish. So, read the documentation for the
DataGrid
. You already have yourDataSet
, so you assign that to theDataGrid.DataSource
property and set theDataGrid.DataMember
property to the table name you want to bind to. Columns are automatically created for each column of that table, but you can control this with theDataGrid.TableStyles
property. Try doing all this in VS.NET using the designer. Create a newDataSet
schema then use the Data toolbox to add a newDataSet
object. Add aDataGrid
to your form or control and play around with some of the properties. Reading the documentation will fill in the details, which are important to understand. There are also many great articles on data-binding here on CodeProject. Just use the search box toward the top and use generic terms like "DataSet" or "DataGrid". I'm sure you'll find plenty. Besides, going into any great deal really isn't meant for the forums. With all the possibilities, this is better discussed in an article. Always be sure to read the documentation, though. It should be your first stop for any questions.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
it seems to be helping a lot! :) I finally made the grid how my data and feel very happy about that. I tho have a couple of last (you wish :)) things to ask (something I didn't yet find in the properties): when the grid comes out it has four columns as expected but how do I make them fill all the space horizontally? the actual grid sits in the left upper filling only about 50% of vertical space. when the text in a column does not fit the grid window horizontally, is there a way to make it put it into a new line? like excel does it? o.w. avoid horizontal scroll bar? and again - thanks a lot! Alexander...
-
it seems to be helping a lot! :) I finally made the grid how my data and feel very happy about that. I tho have a couple of last (you wish :)) things to ask (something I didn't yet find in the properties): when the grid comes out it has four columns as expected but how do I make them fill all the space horizontally? the actual grid sits in the left upper filling only about 50% of vertical space. when the text in a column does not fit the grid window horizontally, is there a way to make it put it into a new line? like excel does it? o.w. avoid horizontal scroll bar? and again - thanks a lot! Alexander...
dophka wrote: (something I didn't yet find in the properties) Don't be a drag-n-drop programmer - read the documentation for the
DataGrid
You'll find a lot more is possible than what the properties provide (some properties are browsable in the designer, and then there's all the methods that you won't see...) dophka wrote: when the grid comes out it has four columns as expected but how do I make them fill all the space horizontally? This is perhaps the biggest problem with theDataGrid
. There's really nothing you can do but resize the column programmatically. I'm sure I've seen a couple articles on this here on the CP site. Just try a search. dophka wrote: when the text in a column does not fit the grid window horizontally, is there a way to make it put it into a new line? like excel does it? First of all, you must understand that Excel is a spreadsheet control - not a grid (just so you don't get any other crazy ieas in your head! :)) Second, you can create a customDataGridColumnStyle
class that allows you to edit text in multiple lines, but the display of the values is dependent on theDataGrid
. To my knowledge, there is no way to get the text to display in multiple lines when not in edit mode.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----