Re: XMLSerialization
Peter Osucha <[email protected]> Thu, 19 Jul 2007 11:29:26 -0400
| Newsgroups | gmane.comp.windows.devel.dotnet.cx |
|---|---|
| Message-ID | <[email protected]> |
Thank you, Craig. Good to know. Peter -----Original Message----- From: Discussion relating to the specifics of the C# and Managed C++ languages [mailto:[email protected]] On Behalf Of Craig Vermeer Sent: Thursday, July 19, 2007 10:30 AM To: [email protected] Subject: Re: [DOTNET-CX] XMLSerialization Peter - This doesn't really answer your question, but I thought I'd point out=20 that the SerializableAttribute you're using on the classes you want to=20 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. > > =20 > > However, it doesn't seem to deserialize at all - I always get an error. > > =20 > > Perhaps someone can help me out here. The code for the project is > below. > > =20 > > 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.. > > =20 > > Peter > > =20 > > =20 > > //Class MyItem... > > using System; > > using System.Collections.Generic; > > using System.Text; > > using System.Drawing; > > =20 > > namespace XMLSerializationTest > > { > > [Serializable] > > public class MyItem > > { > > private string _name; > > private int _number; > > =20 > > public MyItem ( ) { } > > =20 > > public MyItem ( string name, int number ) > > { > > _name =3D name; > > _number =3D number; > > } > > =20 > > public string Name > > { > > get { return _name; } > > set { _name =3D value; } > > } > > =20 > > public int Number > > { > > get { return _number; } > > set { _number =3D value; } > > } > > } > > } > > =20 > > =20 > > // Class MyItemHolder > > using System; > > using System.Collections.Generic; > > using System.Text; > > =20 > > namespace XMLSerializationTest > > { > > [Serializable] > > public class MyItemHolder > > { > > List<MyItem> _myItems =3D new List<MyItem> ( ); > > private string _holderName =3D string.Empty; > > =20 > > public MyItemHolder ( ) { } > > =20 > > public string HolderName > > { > > get { return _holderName; } > > set { _holderName =3D value; } > > } > > =20 > > public List<MyItem> MyItems > > { > > get { return _myItems; } > > set { _myItems =3D value; } > > } > > =20 > > =20 > > } > > } > > =20 > > =20 > > // 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; > > =20 > > namespace XMLSerializationTest > > { > > public partial class Form1 : Form > > { > > private MyItemHolder _holder =3D new MyItemHolder ( ); > > private MyItemHolder _holderRebuild; > > private System.Text.StringBuilder sbHolder =3D new = StringBuilder ( > ); > > =20 > > public Form1 ( ) > > { > > InitializeComponent ( ); > > =20 > > _holder.HolderName =3D "PMO"; > > for ( int i =3D 1; i < 3; i++ ) > > { > > _holder.MyItems.Add ( new MyItem ( "Item..." + > i.ToString ( ), i ) ); > > } > > } > > =20 > > private void button1_Click ( object sender, EventArgs e ) > > { > > System.Xml.Serialization.XmlSerializer xser =3D new > System.Xml.Serialization.XmlSerializer ( typeof ( MyItemHolder ) ); > > =20 > > sbHolder =3D new StringBuilder ( ); > > System.IO.StringWriter sw =3D new System.IO.StringWriter ( > sbHolder ); > > xser.Serialize ( sw, _holder ); > > =20 > > richTextBox1.Text =3D sbHolder.ToString ( ); > > } > > =20 > > private void button2_Click ( object sender, EventArgs e ) > > { > > System.Xml.Serialization.XmlSerializer xser =3D new > System.Xml.Serialization.XmlSerializer ( typeof ( List<MyItem> ) ); > > System.IO.StringReader sr =3D new System.IO.StringReader ( > sbHolder.ToString ( ) ); > > =20 > > try > > { > > _holderRebuild =3D ( MyItemHolder )xser.Deserialize ( = sr > ); > > richTextBox1.Text =3D string.Format ( "Name: {0}, Item > count: {1}", _holderRebuild.HolderName, > _holderRebuild.MyItems.Count.ToString ( ) ); > > } > > catch ( Exception ex ) > > { > > string errorText =3D ex.Message; > > if ( ex.InnerException !=3D null ) errorText =3D = errorText + > "(" + ex.InnerException.Message + ")"; > > richTextBox1.Text =3D errorText; > > } > > =20 > > } > > } > > } > > =20 > > =20 > > > = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > This list is hosted by DevelopMentor(r) http://www.develop.com > > View archives and manage your subscription(s) at http://discuss.develop.com > =20 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D This list is hosted by DevelopMentor(r) http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D This list is hosted by DevelopMentor=AE http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com