 |
C# 2005 For discussion of Visual C# 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

August 25th, 2007, 06:46 PM
|
Authorized User
|
|
Join Date: Feb 2005
Posts: 47
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Filling comboBox from an XML File
Hi all,
Okay I have an XML file that has values in it that I want to populate in a text box. Basically this is the xml file looks.
Code:
<?xml version="1.0" encoding="utf-8"?>
<rs:data xmlns:rs="urn:schemas-microsoft-com:rowset">
<row name="Some feed" value="somefeed.coolfeed.com/topstories.xml"></row>
</rs:data>
The other set of code that I am populating the comboBox is
Code:
private void BindComboBox()
{
string dsFileName = "Feeds.xml";
DataSet dsFeeds = new DataSet();
dsFeeds.ReadXml(dsFileName);
comboBox1.DataSource = dsFeeds;
comboBox1.DisplayMember = "row.name";
comboBox1.ValueMember = "row.value";
}
Now when I took out the fourth line in the second code block, I noticed that there were problems with my xml file. Basically when I went to build the application, there were problems with finding the schema information for row, name, and value. Where could I be going wrong?
Please, please help.
Chris
|

August 26th, 2007, 03:43 PM
|
Authorized User
|
|
Join Date: Feb 2005
Posts: 47
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Hi all,
I think I am just too stupid to figure this out but it has taken me days and I still cant figure out what I am doing wrong. Basically this is a WinForm RSS Reader. I am trying to save the feeds that the user is subscribed to in an xml file that is deployed with the app. I just want those values inside of the file along with the name of those values to be tied to a comboBox control. Basically the names will appear and when the user selects a name I can use the value to point to a feed. Please, please, please tell me where I am going wrong. This problem is very aggravating. I know this shouldn't be all that hard.
Thanks,
Chris
|

August 26th, 2007, 09:09 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
If your XML file is indeed a dataset, you shouldn't need to do much of anything special. You shouldn't need (and I don't believe you can) prefix the DisplayMember or ValueMember of the combobox. Have you set a breakpoint after the ReadXml call and looked at the contents of the dataset to verify that it's reading in the data correctly?
-Peter
|

August 27th, 2007, 04:31 AM
|
Authorized User
|
|
Join Date: Feb 2005
Posts: 47
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Thanks for the reply.
Okay, basically this file is part of my project. How can I be sure that this data is being put into the dataset? I am used to databases and not XML so please be patient. Is there a better way to write my XML file and is a schema necessary? And how do I navigate to the nodes that I want to be the values and names for the comboBox control? I am assuming that for the values and names that I will use XPath to navigate to those values but am not sure. I have changed my XML file to this
Code:
<?xml version="1.0">
<Feeds>
<Feed>name of the feed
<Location>url of feed</Location>
</Feed>
</Feeds>
Pretty much I want to use feed element as the name and location element as the value. Is this workable? Please advise. Your help is greatly appreciated.
Thanks
Chris
|

August 27th, 2007, 08:10 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
I wasn't implying anything was wrong with your original XML file. My concern was that it was being properly loaded into a dataset. If it is, then it's easy to bind that to a control. If you want to work with XML as XML then you need to use one of the xml classes (XmlDocument, XmlTextReader). This complicates things slightly.
-Peter
|

August 28th, 2007, 04:14 AM
|
Authorized User
|
|
Join Date: Feb 2005
Posts: 47
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Thank you so much for the reply.
Basically , it doesn't matter which way I want to read the XML data whether through a typed dataset or directly. Whichever way is the easiest. I know how to add a dataset object to a form, but how do I ensure that it is typed? Please any assistance would be greatly appreciated.
Thanks
Chris
|

August 28th, 2007, 07:01 AM
|
Authorized User
|
|
Join Date: Feb 2005
Posts: 47
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Hi Peter,
Okay I rewrote my code with some success.
Basically this is what I have
Code:
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
string dfFeeds = "Feeds.xml";
dsFeeds.ReadXml(dfFeeds);
cboFeeds.DataSource = dsFeeds.Tables[0];
cboFeeds.DisplayMember = "name";
cboFeeds.ValueMember = "location";
}
}
And for the XML file
Code:
<?xml version = "1.0"?>
<Feeds>
<Feed>
<name></name>
<location></location>
</Feed>
<Feed>
<name></name>
<location></location>
</Feed>
</Feeds>
I say limited success because the first Feed element items appear in the comboBox but not the second. Do I need a foreach loop? Any help would be greatly appreciated.
Thanks
Chris
|

August 28th, 2007, 08:00 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
|

August 28th, 2007, 10:32 AM
|
Authorized User
|
|
Join Date: Feb 2005
Posts: 47
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Thanks very much Peter,
That gives me a good start. But I'm stuck again. Here is my code
[code]
XmlDataDocument feedsXml = new XmlDataDocument();
feedsXml.DataSet.ReadXml(new StreamReader("Feeds.xml"), XmlReadMode.InferSchema);
cboFeeds.DataSource = feedsXml
cboFeeds.DisplayMember = feedsXml.DataSet.Tables[0].Columns[0];
cboFeeds.ValueMember = feedsXml.DataSet.Tables[0].Columns[1];
[\code]
It is saying cannot implicitly convert to string value. I know that display member and value members are string values. So how do I read those values out of the DataSet? And where do I name the dataset. The examples were pretty vague on that detail. Thanks again.
Chris
|

August 28th, 2007, 10:46 AM
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
Change these lines:
cboFeeds.DisplayMember = feedsXml.DataSet.Tables[0].Columns[0];
cboFeeds.ValueMember = feedsXml.DataSet.Tables[0].Columns[1];
to this:
cboFeeds.DisplayMember = feedsXml.DataSet.Tables[0].Columns[0].Caption.ToString()
cboFeeds.ValueMember = feedsXml.DataSet.Tables[0].Columns[1].Caption.ToString();
that should do the trick.
hth.
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
================================================== =========
|
|
 |