[Mono-list] XML problem - please help, this is driving me nuts!

Paul paul at all-the-johnsons.co.uk
Mon May 31 18:01:52 EDT 2010


Hi,

Not sure where I'm messing up here. I'm trying to deserialize some XML.
It's a bog standard deserializer bit of code...

FormData f; // explained below
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);
}

Code works fine. The FormData bit is below.

[Serializable]
[XmlRoot("Forms")]
public class FormList
{
	private List<FormData> forms;
        public FormList()
        {
            forms = new List<FormData>();
        }
        [XmlElement("Form")]
        public FormData[] Forms
        {
            get { return this.forms.ToArray(); }
            set { this.forms = new List<FormData>(value); }
        }
}

[Serializable]
public class FormData
{
        public List<Element> elements;
        public FormData()
        {
            elements = new List<Element>();
        }

        public string Name { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
        public string Title { get; set; }
        public int BackLink { get; set; }
        public int ForwardLink { get; set; }
        public int PageNumber { get; set; }

        [XmlElement("Element")]
        public Element[] Elements
        {
            get { return this.elements.ToArray(); }
            set { this.elements = new List<Element>(value); }
        }
}

[Serializable]
public class Element
{
        [XmlAttribute("Name")]
        public string ElementName { get; set; }
        public int X { get; set; }
        public int Y { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
        public string Title { get; set; }
        public string Text { get; set; }
        
        [XmlAttribute("Type")]
        public string ElementType { get; set; }
        
        [XmlAttribute("External")]
        public bool WithExternal { get; set; }
        public string ExternalFile { get; set; }
        
        [XmlAttribute("Events")]
        public bool HasClick { get; set; }
        public bool HasWritable { get; set; }
        public bool HasNumeric { get; set; }
        public bool HasRO { get; set; }
        
        public Element()
        {
        }      
}

Problem is the XML - the app if throwing back

System.InvalidOperationException : There is an error in the XML document
--> System.InvalidOperationException: <Forms xmlns="> was not expected

The XML looks like this

<?xml version="1.0" encoding="utf-8" ?>
<Forms>
  <Form Name="Test Window" Width="800" Height="350" Title="The eye 1"
BackLink="" ForwardLink="2" PageNumber="1">
    <Name ElementName="pb1" X="25" Y="50" Width="300" Height="200"
Text="" HasClick="false" HasWritable="false" HasNumeric="false"
HasRO="false" />	
    <Type ElementType="System.Windows.Forms.PictureBox" />
    <External WithExternal="true" ExternalFile="h:\eye.jpg" />
    <Events HasClick="false" HasWritable="false" HasNumeric="false"
HasRO="false" />
  </Form>
</Forms>

I'm not sure on what is going wrong as my understanding of XML (which
may be wrong) says that the above should be correct.

HELP!!!!!

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/20100531/f58caa12/attachment-0001.bin 


More information about the Mono-list mailing list