Home All Groups Group Topic Archive Search About

Serialize List of objects as XML

Author
10 May 2006 4:04 PM
Jarod
Hello,

I have a list of objects that I want to serialize as XML and save it as
user setting. The objects are of type of the following class.

    [Serializable]
    public class SourceFolder
    {
        public SourceFolder(string path, bool controlled, bool
includesSub)
        {
            this.path = path;
            this.controlled = controlled;
            this.includesSub = includesSub;
        }

        public string Path
        {
            get
            {
                return path;
            }
            set
            {
                path = value;
            }
        }

        public bool Controlled
        {
            get
            {
                return controlled;
            }
            set
            {
                controlled = value;
            }
        }

        public bool IncludesSub
        {
            get
            {
                return includesSub;
            }
            set
            {
                includesSub = value;
            }
        }

        private string path;
        private bool controlled;
        private bool includesSub;
    }

In my settings object, I have a property of type List<SourceFolder>
that I assign my object list before saving. Unfortunately, the only
output in the file user.config is the following:

<setting name="SourceFolders" serializeAs="Xml">
    <value />
</setting>

When I add the attribute
SettingsSerializeAs(SettingsSerializeAs.Binary) to the settings
property, it works, but the list is serialized binary what I do not
want.

Visual Studio 2005 help states that .NET framework is able to serialize
lists of any object to XML stream. What am I doing wrong that it does
not work for me? When I use List<string> instead of List<SourceFolder>,
it works fine, but not with my class.


Regards, Jens

Author
10 May 2006 5:47 PM
sloan
I have some 1.1 code at:
http://spaces.msn.com/sholliday/           9/21/2005

I haven't done anything with 2.0 and generics.

Maybe it can help, maybe not.




Show quote
"Jarod" <ja***@onlinehome.de> wrote in message
news:1147277067.169713.293270@i39g2000cwa.googlegroups.com...
> Hello,
>
> I have a list of objects that I want to serialize as XML and save it as
> user setting. The objects are of type of the following class.
>
>     [Serializable]
>     public class SourceFolder
>     {
>         public SourceFolder(string path, bool controlled, bool
> includesSub)
>         {
>             this.path = path;
>             this.controlled = controlled;
>             this.includesSub = includesSub;
>         }
>
>         public string Path
>         {
>             get
>             {
>                 return path;
>             }
>             set
>             {
>                 path = value;
>             }
>         }
>
>         public bool Controlled
>         {
>             get
>             {
>                 return controlled;
>             }
>             set
>             {
>                 controlled = value;
>             }
>         }
>
>         public bool IncludesSub
>         {
>             get
>             {
>                 return includesSub;
>             }
>             set
>             {
>                 includesSub = value;
>             }
>         }
>
>         private string path;
>         private bool controlled;
>         private bool includesSub;
>     }
>
> In my settings object, I have a property of type List<SourceFolder>
> that I assign my object list before saving. Unfortunately, the only
> output in the file user.config is the following:
>
> <setting name="SourceFolders" serializeAs="Xml">
>     <value />
> </setting>
>
> When I add the attribute
> SettingsSerializeAs(SettingsSerializeAs.Binary) to the settings
> property, it works, but the list is serialized binary what I do not
> want.
>
> Visual Studio 2005 help states that .NET framework is able to serialize
> lists of any object to XML stream. What am I doing wrong that it does
> not work for me? When I use List<string> instead of List<SourceFolder>,
> it works fine, but not with my class.
>
>
> Regards, Jens
>
Author
11 May 2006 3:39 PM
Jarod
Hello,

In the meantime I have solved the problem. I made a wrapper class
SourceFolderList which contains the List<SourceFolder>. Additionally I
had to assign the attribute XmlInclude(typeof(SourceFolder)) to this
class and now it works fine.


Jens

AddThis Social Bookmark Button