Discussion:
XMLSerialization
Peter Osucha
2007-07-18 20:48:43 UTC
Permalink
Adam Sills
2007-07-18 21:14:01 UTC
Permalink
Peter Osucha
2007-07-19 01:11:43 UTC
Permalink
Peter Osucha
2007-07-19 02:39:33 UTC
Permalink
Adam Sills
2007-07-19 14:33:15 UTC
Permalink
Peter Osucha
2007-07-19 15:30:50 UTC
Permalink
Peter Osucha
2007-08-20 15:36:45 UTC
Permalink
Peter Osucha
2007-08-22 13:11:07 UTC
Permalink
Craig Vermeer
2007-07-19 14:30:21 UTC
Permalink
Peter -

This doesn't really answer your question, but I thought I'd point out
that the SerializableAttribute you're using on the classes you want to
serialize isn't needed. XmlSerializer doesn't look at this attribute...
it's a fairly common misconception, actually.

Thanks,
Craig

Peter Osucha wrote:
> I have been playing around with this again as a way to import/export
> some objects. It seems to work pretty well (I'm just marking the
> classes as [Serializable] for now). I have had no trouble serializing a
> simple class as well as a simple List<>. The I combined them so that I
> had a simple class with a List<> in side of It and it still seems to
> have serialized OK.
>
>
>
> However, it doesn't seem to deserialize at all - I always get an error.
>
>
>
> Perhaps someone can help me out here. The code for the project is
> below.
>
>
>
> There is a class MyItem with two properties. There is a class
> MyItemHolder with a two properties, one being a List<MyItem>. Then
> there is a form with two buttons on it, one to serialize, the second to
> deserialize and a richtextbox to show results..
>
>
>
> Peter
>
>
>
>
>
> //Class MyItem...
>
> using System;
>
> using System.Collections.Generic;
>
> using System.Text;
>
> using System.Drawing;
>
>
>
> namespace XMLSerializationTest
>
> {
>
> [Serializable]
>
> public class MyItem
>
> {
>
> private string _name;
>
> private int _number;
>
>
>
> public MyItem ( ) { }
>
>
>
> public MyItem ( string name, int number )
>
> {
>
> _name = name;
>
> _number = number;
>
> }
>
>
>
> public string Name
>
> {
>
> get { return _name; }
>
> set { _name = value; }
>
> }
>
>
>
> public int Number
>
> {
>
> get { return _number; }
>
> set { _number = value; }
>
> }
>
> }
>
> }
>
>
>
>
>
> // Class MyItemHolder
>
> using System;
>
> using System.Collections.Generic;
>
> using System.Text;
>
>
>
> namespace XMLSerializationTest
>
> {
>
> [Serializable]
>
> public class MyItemHolder
>
> {
>
> List<MyItem> _myItems = new List<MyItem> ( );
>
> private string _holderName = string.Empty;
>
>
>
> public MyItemHolder ( ) { }
>
>
>
> public string HolderName
>
> {
>
> get { return _holderName; }
>
> set { _holderName = value; }
>
> }
>
>
>
> public List<MyItem> MyItems
>
> {
>
> get { return _myItems; }
>
> set { _myItems = value; }
>
> }
>
>
>
>
>
> }
>
> }
>
>
>
>
>
> // Form code (a richtext box to show results, button1 to serialize,
> button2 to deserialize)
>
> using System;
>
> using System.Collections.Generic;
>
> using System.ComponentModel;
>
> using System.Data;
>
> using System.Drawing;
>
> using System.Text;
>
> using System.Windows.Forms;
>
> using System.Xml;
>
> using System.Xml.Serialization;
>
> using System.IO;
>
>
>
> namespace XMLSerializationTest
>
> {
>
> public partial class Form1 : Form
>
> {
>
> private MyItemHolder _holder = new MyItemHolder ( );
>
> private MyItemHolder _holderRebuild;
>
> private System.Text.StringBuilder sbHolder = new StringBuilder (
> );
>
>
>
> public Form1 ( )
>
> {
>
> InitializeComponent ( );
>
>
>
> _holder.HolderName = "PMO";
>
> for ( int i = 1; i < 3; i++ )
>
> {
>
> _holder.MyItems.Add ( new MyItem ( "Item..." +
> i.ToString ( ), i ) );
>
> }
>
> }
>
>
>
> private void button1_Click ( object sender, EventArgs e )
>
> {
>
> System.Xml.Serialization.XmlSerializer xser = new
> System.Xml.Serialization.XmlSerializer ( typeof ( MyItemHolder ) );
>
>
>
> sbHolder = new StringBuilder ( );
>
> System.IO.StringWriter sw = new System.IO.StringWriter (
> sbHolder );
>
> xser.Serialize ( sw, _holder );
>
>
>
> richTextBox1.Text = sbHolder.ToString ( );
>
> }
>
>
>
> private void button2_Click ( object sender, EventArgs e )
>
> {
>
> System.Xml.Serialization.XmlSerializer xser = new
> System.Xml.Serialization.XmlSerializer ( typeof ( List<MyItem> ) );
>
> System.IO.StringReader sr = new System.IO.StringReader (
> sbHolder.ToString ( ) );
>
>
>
> try
>
> {
>
> _holderRebuild = ( MyItemHolder )xser.Deserialize ( sr
> );
>
> richTextBox1.Text = string.Format ( "Name: {0}, Item
> count: {1}", _holderRebuild.HolderName,
> _holderRebuild.MyItems.Count.ToString ( ) );
>
> }
>
> catch ( Exception ex )
>
> {
>
> string errorText = ex.Message;
>
> if ( ex.InnerException != null ) errorText = errorText +
> "(" + ex.InnerException.Message + ")";
>
> richTextBox1.Text = errorText;
>
> }
>
>
>
> }
>
> }
>
> }
>
>
>
>
>
>
> ===================================
> This list is hosted by DevelopMentorĀ® http://www.develop.com
>
> View archives and manage your subscription(s) at http://discuss.develop.com
>

===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Peter Osucha
2007-07-19 15:29:26 UTC
Permalink
Loading...