[Mono-list] XML deserialization - HELP!!!!!
Paul
paul at all-the-johnsons.co.uk
Mon May 24 18:21:09 EDT 2010
Hi,
As part of a much larger project, I'm doing a simple proof of concept
work. Basically, the code I have loads in an XML file and generates a
winform on the fly.
The code is at a very early stage, but I've hit a problem. It compiles,
it runs, it just completely fails to load in the XML file (well,
something happens, but not what I'm after which should be a winform
generated based on the XML file.
Current code looks like this
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
namespace form_from_xml
{
public class xmlhandler : Form
{
public void loaddesign()
{
FormData f;
f = null;
try
{
string path_env =
Path.GetDirectoryName(Application.ExecutablePath) +
Path.DirectorySeparatorChar;
XmlSerializer s = new XmlSerializer(typeof(FormData));
TextReader r = new StreamReader(path_env +
"designer-test.xml");
f = (FormData)s.Deserialize(r);
r.Close();
}
catch (System.IO.FileNotFoundException)
{
MessageBox.Show("Unable to find the form file", "File
not found", MessageBoxButtons.OK);
}
catch (System.InvalidOperationException e)
{
MessageBox.Show(e.ToString(), "Error",
MessageBoxButtons.OK);
}
winformgen wf = new winformgen();
wf.makeform(f);
}
}
public class winformgen : Form
{
public void makeform(FormData f)
{
Form form1 = new Form();
Label label1 = new Label();
label1.Text = f.elements[0].text;
label1.Location = new Point(f.elements[0].xpos,
f.elements[0].ypos);
form1.FormBorderStyle = FormBorderStyle.FixedDialog;
form1.MaximizeBox = false;
form1.MinimizeBox = false;
form1.StartPosition = FormStartPosition.CenterScreen;
form1.Controls.Add(label1);
form1.Width = f.elements[0].winxs;
form1.Height = f.elements[0].winys;
form1.ShowDialog();
}
}
[XmlRoot("Forms")]
public class FormData
{
private ArrayList formData;
public FormData()
{
formData = new ArrayList();
}
[XmlElement("Element")]
public Elements[] elements
{
get
{
Elements[] elements = new Elements[formData.Count];
formData.CopyTo(elements);
return elements;
}
set
{
if (value == null)
return;
Elements[] elements = (Elements[])value;
formData.Clear();
foreach (Elements element in elements)
formData.Add(element);
}
}
}
public class Elements
{
[XmlAttribute("formname")]
public string name;
[XmlAttribute("winxsize")]
public int winxs;
[XmlAttribute("winysize")]
public int winys;
[XmlAttribute("type")]
public string type;
[XmlAttribute("xpos")]
public int xpos;
[XmlAttribute("ypos")]
public int ypos;
[XmlAttribute("externaldata")]
public bool external;
[XmlAttribute("externalplace")]
public string externalplace;
[XmlAttribute("text")]
public string text;
[XmlAttribute("questions")]
public bool questions;
[XmlAttribute("questiontype")]
public string qtype;
[XmlAttribute("numberqs")]
public int numberqs;
[XmlAttribute("answerfile")]
public string ansfile;
[XmlAttribute("backlink")]
public int backlink;
[XmlAttribute("forwardlink")]
public int forwardlink;
public Elements()
{
}
public Elements(string fn, int wx, int wy, string t, int x, int
y, bool ext, string extpl, string te, bool q,
string qt, int num, string ans, int back, int end)
{
name = fn;
winxs = wx;
winys = wy;
type = t;
xpos = x;
ypos = y;
external = ext;
externalplace = extpl;
text = te;
questions = q;
qtype = qt;
numberqs = num;
ansfile = ans;
backlink = back;
forwardlink = end;
}
}
}
With the XML file
<?xml version="1.0" encoding="utf-8"?>
<Forms>
<Element>
<formname>Test Window</formname>
<winxsize>300</winxsize>
<winysize>500</winysize>
<type>Label</type>
<xpos>100</xpos>
<ypos>100</ypos>
<externaldata>true</externaldata>
<externalplace>none</externalplace>
<text>Hello world!</text>
<questions>true</questions>
<questiontype>none</questiontype>
<numberqs>0</numberqs>
<answerfile>none</answerfile>
<backlink>999</backlink>
<forwardlink>999</forwardlink>
</Element>
</Forms>
(to get it to work, a quick winform button which generates a new
instance of xmlhandler then call the new instance)
I can't for the life of me see why it's not reading in. The only thing I
can think of is that there is a problem in the XML file somewhere...
Any help would be greatly appreciated :-)
TTFN
Paul
--
Biggles was quietly reading his favourite book when Algy burst through
the door. Distracted for a moment, Biggles surveyed what had happened
and turned a page. "Algy old man" he said, clearing his throat, "use the
handle next time..." - Taken from "Biggles combs his Hair"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
Url : http://lists.ximian.com/pipermail/mono-list/attachments/20100524/724b9f4f/attachment-0001.bin
More information about the Mono-list
mailing list