Results 1 to 7 of 7

Thread: [RESOLVED] Serialized XML Classes - InvalidOperationException

  1. #1

    Thread Starter
    Frenzied Member TomGibbons's Avatar
    Join Date
    Feb 2002
    Location
    San Diego, CA Previous Location: UK
    Posts
    1,345

    Resolved [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?
    Attached Files Attached Files
    Last edited by TomGibbons; Mar 31st, 2005 at 12:49 PM.

  2. #2

    Thread Starter
    Frenzied Member TomGibbons's Avatar
    Join Date
    Feb 2002
    Location
    San Diego, CA Previous Location: UK
    Posts
    1,345

    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

  3. #3
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    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;
    	}
    }

  4. #4

    Thread Starter
    Frenzied Member TomGibbons's Avatar
    Join Date
    Feb 2002
    Location
    San Diego, CA Previous Location: UK
    Posts
    1,345

    Re: Serialized XML Classes - InvalidOperationException

    I've removed the XmlAttribute bits from there too...

  5. #5
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    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;

  6. #6

    Thread Starter
    Frenzied Member TomGibbons's Avatar
    Join Date
    Feb 2002
    Location
    San Diego, CA Previous Location: UK
    Posts
    1,345

    Re: Serialized XML Classes - InvalidOperationException

    ooooooooooooh Christ almighty you're right. I can't believe I didn't see that. Thank you!

  7. #7
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    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!
    np

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width