|
-
June 16th, 2011, 09:08 AM
#1
create a windowsform from an xml file..
not sure how to go about this.. this is my second project so any advise where to look for, etc will be much appreciated..
so the project is, create a windows form (with text fields, drop downs, etc) where all the settings (size, position, etc of the text fields) are to be read from an xml file..
i've gotten comfortable coding a console app.. now this is using the windows form.. so all new..
creating the form using the form designer is easy (drag, drop, etc), but the appearance of the this form with these text fields (for input) will be based on an xml file
thanks again in advance..
-
June 17th, 2011, 02:15 AM
#2
Re: create a windowsform from an xml file..
WPF(Silverlight) forms are represented with XAML, XAML is actually a kind of xml.
-
June 17th, 2011, 05:22 PM
#3
Re: create a windowsform from an xml file..
It is kind of open-ended at the moment as to what you need to do. Are you supposed to create controls dynamically using data read from XML and place them on a new form? Or, would your controls already exist on the form, and you just arrange them and set their content based on XML data?
What is it you are unsure of initially?
- How to create an XML file?
- How to read an XML file?
- How to create a control dynamically in the constructor or load event?
- How to move controls to a given location?
Start with a specific question or problem, and perhaps you will find a few more willing contributors? ... just my thought.
-
June 21st, 2011, 02:41 PM
#4
Re: create a windowsform from an xml file..
thanks all..
ok.. i'll start by this.. i want to read values from an xml file..
for example, the xml file is like this:
<config>
<label>
<label_name>lblName</label_name>
<label_text>Name</label_text>
<label_width>79</label_width>
<label_height>13</label_height>
<label_location_x>8</label_location_x>
<label_location_y>17</label_location_y>
</label>
<text>
<text_name>txtName</text_name>
<text_text>blank</text_text>
<text_width>265</text_width>
<text_height>20</text_height>
<text_location_x>126</text_location_x>
<text_location_y>14</text_location_y>
</text>
</config>
how do i read the set from the node <label> and set from <text>
i want to read from the top to bottom something like this:
XmlDataDocument xmldoc = new XmlDataDocument();
XmlNodeList xmlnode;
int i = 0;
string str = null;
FileStream fs = new FileStream("config.xml", FileMode.Open, FileAccess.Read);
xmldoc.Load(fs);
then a code here to loop through the entire xml file reading from the top
then find each node and their child nodes and values and assign it to the variables in the code
thanks..
-
June 21st, 2011, 05:22 PM
#5
Re: create a windowsform from an xml file..
Okay, so... below, you will find a small example first of how you can read in your xml, then loop through the child nodes of <config> to handle each control render according to the data you gather.
Code:
static void ReadXML_Ex1()
{
// this is how you load an xml document object from a file like yours...
XmlDocument doc = new XmlDocument();
doc.Load(@"j:\test.xml");
if (doc.HasChildNodes)
{
if (doc.SelectSingleNode("config").HasChildNodes)
{
foreach (XmlNode controlNode in doc.SelectSingleNode("config").ChildNodes)
{
// check for type of control by element name and handle child node values as needed.
// your name, text, width, etc under this node (childnodes) are values you want to store/use to create/position the control.
Console.WriteLine("This is a " + controlNode.Name + " control element.");
}
}
}
}
Following, I have posted a new sample of XML data that may be better suited for parsing and processing your dynamic control data... there are a lot of resources online regarding XML and various ways to construct your file and query the data within... it can be well worth the time to get a handle on that information to save yourself a lot of extra code in the future. 
First, I'll list my second version of your XML data, then the code for the second method to read the new file and handle the various controls within. Note, I didn't go all-out handling your control types for you etc, that leaves lots of learning to do about XML and design on your part... but I hope it gives you a good idea of what is possible.
New XML Listing
Code:
<?xml version="1.0" encoding="utf-8" ?>
<config>
<controls>
<control type="label">
<properties>
<name>lblName</name>
<content type="text">Name</content>
<location x="8" y="17"></location>
<dimensions width="79" height="13"></dimensions>
</properties>
</control>
<control type="textbox">
<properties>
<name>tbName</name>
<content type="text"></content>
<location x="126" y="14"></location>
<dimensions width="265" height="20"></dimensions>
</properties>
</control>
<control type="listbox">
<properties>
<name>lbAge</name>
<content type="list">
<item>20</item>
<item>21</item>
<item>22</item>
</content>
<location x="50" y="17"></location>
<dimensions width="250" height="20" dropwidth="300"></dimensions>
</properties>
</control>
</controls>
</config>
Second Example Method for New XML
Code:
static void ReadXML_Ex2()
{
// this is how you load an xml document object from a file such as the new example I posted...
XmlDocument doc = new XmlDocument();
doc.Load(@"j:\test2.xml");
// process all the controls in the xml...
foreach (XmlNode controlNode in doc.SelectNodes("config/controls/control"))
{
switch (controlNode.Attributes["type"].Value)
{
case "label":
Console.WriteLine("label");
break;
case "textbox":
Console.WriteLine("textbox");
break;
case "listbox":
Console.WriteLine("listbox");
break;
}
}
}
Best of luck!
-
June 22nd, 2011, 10:16 AM
#6
Re: create a windowsform from an xml file..
fcornin.. thanks so much.. i'll try this out once i'm done documenting another app and post here the results.. again, much appreciated..
-
June 22nd, 2011, 10:38 AM
#7
Re: create a windowsform from an xml file..
quick question: how do i loop thru the nodes under label?
in the xml file:
<control type="label">
<properties>
<name>lblName</name>
<content type="text">Name</content>
<location x="8" y="17"></location>
<dimensions width="79" height="13"></dimensions>
</properties>
from the code:
switch (controlNode.Attributes["type"].Value)
{
case "label":
Console.WriteLine("label");
break;
How do i extract the node "name" and the value "lblName"?
I wanted to do where if i have a variable string = labelName, I can do this:
if node is "name" then labelName = lblName..
and so on (to read the next nodes inside or within the <properties> tag under the <control type = "label">
thanks again
-
June 22nd, 2011, 01:30 PM
#8
Re: create a windowsform from an xml file..
Out of curiosity, why reinvent the wheel when WPF already has the capability to dynamically read xml (xaml) on the fly?
Btw, if you are looking for the WinForms look and feel (instead of the more modern WPF look), you can style WPF to look like WinForms.
-
June 22nd, 2011, 03:38 PM
#9
Re: create a windowsform from an xml file..
i'm not familiar with WPF.. i started reading about it and it's nice..
the challenge in this project is to have a (somewhat) easy config file to open, read and change by the user (client).. assumption is the user have minimal knowhow on other things aside from what's their job/task(s).
so providing them with a simple xml file that can be opened in notepad, show them how to add items, or change items and save it, then run the application and they can have their own 'customized' (form) app (with text boxes and buttons) that they can do their data entry.
maybe in the future, i can utilize this WPF in future assignments.
thanks
-
June 23rd, 2011, 01:38 PM
#10
Re: create a windowsform from an xml file..
Well, I could keep posting more and more code... but, let's keep it a learning experience at this point, and do this with hints. 
The best way to pick up what you need is by exploring google results on "c# reading xml", in addition to just playing with the xml classes and their properties/methods to discover their structure and how the objects in those classes relate to each other.
That said, the basic idea is that what you are iterating through is like a treeview, where you have a 'root' node, that node has 'child' nodes, and each of those children can have their own 'child' nodes, etc etc etc...
Since you are interested in the items under the <control> element, consider those items the children of the control node, and play with the first code sample I provided to see if you can go through those children and get the values you want.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|