[Mono-list] Code is driving me nuts!

Paul paul at all-the-johnsons.co.uk
Wed May 19 18:33:34 EDT 2010


Hi,

For some reason code that I've been using for ages and in a number of
different projects is currently throwing a wobbler. (The
error is an InvalidOperationException in the XmlSerializer line).

My brain is currently in the mush phase as I'm up to my neck in
getting students through their Chemistry and Biology exams so I can't
see anything obvious - any pointers to the problem would be
appreciated.

The code is a proof of concept sort of thing to load in an XML file
and generate widgets and windows and the likes (including events) from
the XML file (this is the easy bit, the hard bit is the designer
part)...

I'm having to do things this way as the code I'd like to use for window
generation and subsequent import won't work with mono due to problems
with unsafe code...

TTFN

Paul

8-->
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 partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void loaddesign()
        {
            FormData f;
                        f = null;
            try
                        {
                string path_env =
Path.GetDirectoryName(Application.ExecutablePath) +
Path.DirectorySeparatorChar;
                                XmlSerializer s = new
XmlSerializer( typeof( FormData ) ); // dies
here, has problems reflecting type FormData
                                TextReader r = new
StreamReader( "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);
                        }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            loaddesign();
        }
    }

    [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 int AddItem(Elements element)
        {
            return formData.Add(element);
        }
    }

    public class Elements
    {
        [XmlAttribute("formname")]
        public string name;
        [XmlAttribute("type")]
        public object 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 object 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, object t, int x, int y, bool ext,
string extpl, string te, bool q, object qt, int num,
            string ans, int back, int end)
        {
            name = fn;
            type = t;
            xpos = x;
            ypos = y;
            external = ext;
            externalplace = extpl;
            text = te;
            questions = q;
            qtype = qt;
            numberqs = num;
            ansfile = ans;
            backlink = back;
            forwardlink = end;
        }
    }
}
<--8
-- 
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/20100519/aa9516fb/attachment.bin 


More information about the Mono-list mailing list