473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

simple xml for states list

Hi,

Quick Question:
How to create a simple XML with US States list and read it into a
combo box.

Detail:
I am very new to Dot Net technology as well as xml.
I am building a desktop app in C# and need to populate the US States
list (AL, AR, .. WY) in a combo box. I could have done it by reading
from a table in SQL server but rather prefer to have that list saved
locally at client side as an xml and read it from there. what is the
simplest way to go about it, what should be the structure of xml
document and do i need schema also ?
thanks for any response

Aamir
Nov 11 '05 #1
3 7248
My answer for (almost) everything: XML Serialization.

here's how I would do it for this simple scope:
1. Create an XML file (maybe by querying a database) that represents the
data you want.
it might look something like this:
<StateList>
<State abbrev="AK" name="Alaska" />
<State abbrev="AL" name="Alabama" />
<State abbrev="AR" name="Arkansas" />
...
</StateList>

2. infer a schema from the xml file using the xsd.exe util in the .NET
Framework SDK. note: check the options on xsd.exe to select the
code-generation language (VB or C#).
xsd.exe /c statelist.xml

3. Compile the generated code

4. Write your app that uses the generated classes. To populate the
instances, use xml serialization.
something like this:

System.IO.FileStream fs = new System.IO.FileStream(path,
System.IO.FileMode.Open);
System.Xml.Serialization.XmlSerializer s= new
System.Xml.Serialization.XmlSerializer(typeof(Stat eList));
StateList l= (StateList) s.Deserialize(fs);
fs.Close();

5. You then can bind this thing to a control, like a listbox.

If I am not mistaken, the databound controls require public properties, not
fields. The xsd.exe tool generates classes with public fields. You may
need to modify the generated classes, or take some other step to
workaround. The issue is discussed in more detail here:
http://msdn.microsoft.com/library/en...ml01202003.asp

An alternative workaround is shown here:
http://www.winisp.net/cheeso/srcview...=MorphArray.cs

-Dino
"Aamir" <gh*****@bigfoot.com> wrote in message
news:4a************************@posting.google.com ...
Hi,

Quick Question:
How to create a simple XML with US States list and read it into a
combo box.

Detail:
I am very new to Dot Net technology as well as xml.
I am building a desktop app in C# and need to populate the US States
list (AL, AR, .. WY) in a combo box. I could have done it by reading
from a table in SQL server but rather prefer to have that list saved
locally at client side as an xml and read it from there. what is the
simplest way to go about it, what should be the structure of xml
document and do i need schema also ?
thanks for any response

Aamir

Nov 11 '05 #2
Hi Dino

thanks for the post.
Meanwhile I worked out a solution as follows
created an xml file by using FOR XML clause in sql statement.
in my C# program I read the file through
FileStream through XMLTextReader
Used ReadXml method of empty dataset object to read the XMLTextReader
object into dataset.
Bind the DisplayMember & ValueMember properties to the dataset table
columns.
The detail is as follows.

Why would you prefer the serilazation over this method I followed.
Will appreciate your comment.

Aamir

//My Solution:
// Create a new DataSet.
DataSet dsStates= new DataSet();
// Read the XML document back in.
// Create new FileStream to read schema with.
System.IO.FileStream fsReadXml = new
System.IO.FileStream("USStates.xml", System.IO.FileMode.Open);
// Create an XmlTextReader to read the file.
System.Xml.XmlTextReader myXmlReader =
new System.Xml.XmlTextReader(fsReadXml);
// Read the XML document into the
DataSet.dsStates.ReadXml(myXmlReader);
cboState.DataSource = dsStates;
cboState.DisplayMember = "Row.State";
cboState.ValueMember = "Row.State";


*******************
"Dino Chiesa [MSFT]" <di****@microsoft.com> wrote in message news:<#6**************@tk2msftngp13.phx.gbl>...
My answer for (almost) everything: XML Serialization.......

Nov 11 '05 #3
The DataSet is a fine way to do it, especially if your data originates in a
database.
-Dino

"Aamir" <gh*****@bigfoot.com> wrote in message
news:4a**************************@posting.google.c om...
Hi Dino

thanks for the post.
Meanwhile I worked out a solution as follows
created an xml file by using FOR XML clause in sql statement.
in my C# program I read the file through
FileStream through XMLTextReader
Used ReadXml method of empty dataset object to read the XMLTextReader
object into dataset.
Bind the DisplayMember & ValueMember properties to the dataset table
columns.
The detail is as follows.

Why would you prefer the serilazation over this method I followed.
Will appreciate your comment.

Aamir

//My Solution:
// Create a new DataSet.
DataSet dsStates= new DataSet();
// Read the XML document back in.
// Create new FileStream to read schema with.
System.IO.FileStream fsReadXml = new
System.IO.FileStream("USStates.xml", System.IO.FileMode.Open);
// Create an XmlTextReader to read the file.
System.Xml.XmlTextReader myXmlReader =
new System.Xml.XmlTextReader(fsReadXml);
// Read the XML document into the
DataSet.dsStates.ReadXml(myXmlReader);
cboState.DataSource = dsStates;
cboState.DisplayMember = "Row.State";
cboState.ValueMember = "Row.State";


*******************
"Dino Chiesa [MSFT]" <di****@microsoft.com> wrote in message

news:<#6**************@tk2msftngp13.phx.gbl>...
My answer for (almost) everything: XML Serialization.......

Nov 11 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael Champagne | last post by:
Does anyone have any examples or links to examples of generating a simple unordered list menu with multiple levels from an XML file? We are redesigning the navigation on our website and I was...
3
by: mark | r | last post by:
im lookinf for an idiots guide to creating a product list where categories are stored in a separate table from the products table, i know how to link the data in access so the products table is...
5
by: disco | last post by:
I am working on this example from a book "C Primer Plus" by Prata 4th edition - p. 672. There is no erata on this problem at the publisher's website. 1) Is it a violation of copyright laws to...
13
by: na1paj | last post by:
here's a simple linked list program. the DeleteNode function is producing an infinit loop i think, but i can't figure out where.. #include <stdio.h> typedef struct { char *str; //str is a...
1
by: ImOk | last post by:
I am looking for a simple mailing list and label maker application in PHP. The main code I want is the editing and lookup of records as I will add the label printing. Maybe someone knows of a...
3
by: cowznofsky | last post by:
I'm getting data in a generic list class, which I'm not going to change. I would like to use it as a datasource for a datagrid or a gridview, but it doesn't implement IEnumerable. I'm...
1
by: jyothiram | last post by:
write short notes on linked list resentation using arrays,pointers and cursors?
4
by: phe2003 | last post by:
Hi All, I've been testing some extremely simple code about a linked list. What I keep doing is actually writing some parts of a linked list and testing them. Below are my codes:...
0
by: babe20042004 | last post by:
My function round1(boolean qualify, int goals) is supposed to go through a list of nodes comparing every 2 side by side nodes for the highest goals and then has the boolean variable qualify changed...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.