1 Attachment(s)
[RESOLVED] Serialized XML Classes - InvalidOperationException
I have the following attached class. When I use the following code in a form:
Code:
XmlList myList = new XmlList();
myList.AddItem(new List("test1","test2"));
myList.AddItem(new List("test3","test4"));
myList.AddItem(new List("test5","test6"));
XmlSerializer s = new XmlSerializer( (typeof(XmlList)) );
TextWriter w = new StreamWriter( "list.xml" );
s.Serialize( w, myList );
w.Close();
XmlList newList;
TextReader r = new StreamReader( "list.xml" );
newList = (XmlList)s.Deserialize( r );
r.Close();
It throws an InvalidOperationException and says: "There was an error reflecting type 'XmlList' " on the highlighted line above.
Any ideas? :(
Re: Serialized XML Classes - InvalidOperationException
Well I got it to work fine by removing all of the [XmlRoot] and [XmlAttribute] bits. But I shan't mark this as resolved because I'd stil like to know what was causing the original problem :)
Re: Serialized XML Classes - InvalidOperationException
You're not going to be happy ;)
In XmlList.cs:
Code:
public class List
{
[XmlAttribute("item1")] public string Item1;
[XmlAttribute("item1")] public string Item2;
public List()
{
}
public List(string Item1, string Item2)
{
this.Item1 = Item1;
this.Item2 = Item2;
}
}
Re: Serialized XML Classes - InvalidOperationException
I've removed the XmlAttribute bits from there too...
Re: Serialized XML Classes - InvalidOperationException
Quote:
Originally Posted by TomGibbons
I've removed the XmlAttribute bits from there too...
Sorry, should've been a little clearer :) The problem was that you had two elements exposed with the same name "item1"; thus no need to remove the XML serialisation attributes, just replace with the following:
Code:
[XmlAttribute("item1")] public string Item1;
[XmlAttribute("item2")] public string Item2;
Re: Serialized XML Classes - InvalidOperationException
ooooooooooooh Christ almighty you're right. I can't believe I didn't see that. Thank you! :D
Re: Serialized XML Classes - InvalidOperationException
Quote:
Originally Posted by TomGibbons
ooooooooooooh Christ almighty you're right. I can't believe I didn't see that. Thank you! :D
np :)