XMLSerializer generates "There was an error generating the XML document".

In a project I'm writing, one requirement is to serialize the class structure to an XML stream.  However, the hierarchy of the classes is causing problems when I (XML) serialize them.  I get an "There was an error generating the XML document" error which isn't terribly useful and I don't understand why.

I've attached the following code for review.
1. The code that generates the error
2. The group class that is used as a "bucket" to store user entries.  Groups can contain collections of entries and other groups.  I suspect this relationship is contributing to the error I'm getting.
3. The entry class that stores user information about a site.

KeySafeGroup group = new KeySafeGroup("Root");
            group.Groups.Add("TestGroup1", new KeySafeGroup("TestGroup"));
            group.Entries.Add("TestEntry1", new KeySafeEntry("TestEntry1", "test_username", "test_password", "test_url", "test_notes"));
 
            XmlSerializer xmls = new XmlSerializer(group.GetType());
            try
            {
                xmls.Serialize(Console.Out, group);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
 
 
/******************************************************************/
 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
 
[XmlRoot("KeySafeEntry")]
public class KeySafeEntry
{
    private string _entryName;
    private string _username;
    private string _password;
    private string _url;
    private string _notes;
 
    public KeySafeEntry()
    { }
 
    public KeySafeEntry(string entryName, string userName, string passWord, string entryUrl, string entryNotes)
    {
        this.EntryName = entryName;
        this.Username = userName;
        this.Password = passWord;
        this.Url = entryUrl;
        this.Notes = entryNotes;
    }
 
    public string EntryName
    {
        get { return this._entryName; }
        set { this._entryName = value; }
    }
 
    public string Username
    {
        get { return this._username; }
        set { this._username = value; }
    }
 
    public string Password
    {
        get { return this._password; }
        set { this._password = value; }
    }
 
    public string Url
    {
        get { return this._url; }
        set { this._url = value; }
    }
 
    public string Notes
    {
        get { return this._notes; }
        set { this._notes = value; }
    }
}
 
public class KeySafeEntries : Dictionary<string, KeySafeEntry> { }
 
/******************************************************************/
 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
 
[XmlRoot("KeySafeGroup")]
public class KeySafeGroup
{
    private string _groupName;
    private Dictionary<string, KeySafeGroup> _groups = new Dictionary<string, KeySafeGroup>();
    private Dictionary<string, KeySafeEntry> _entries = new Dictionary<string, KeySafeEntry>();
 
    public KeySafeGroup()
    { }
 
    public KeySafeGroup(string groupName)
    {
        this._groupName = groupName;
    }
 
    public string GroupName
    {
        get { return this._groupName; }
        set { this._groupName = value; }
    }
 
    [XmlIgnore()]
    public Dictionary<string, KeySafeGroup> Groups
    {
        get { return this._groups; }
        set { this._groups = value; }
    }
 
    [XmlIgnore()]
    public Dictionary<string, KeySafeEntry> Entries
    {
        get { return this._entries; }
        set { this._entries = value; }
    }
 
    [XmlArray("Entries")]
    [XmlArrayItem("EntryItem", Type = typeof(DictionaryEntry))]
    public DictionaryEntry[] _x_Entry
    {
        get 
        {
            DictionaryEntry[] ret = new DictionaryEntry[this.Entries.Count];
            int i = 0;
            foreach (KeyValuePair<string, KeySafeEntry> entryItem in this.Entries)
            {
                DictionaryEntry de = new DictionaryEntry { Key = entryItem.Key, Value = entryItem.Value };
                ret[i] = de;
                i++;
            }
 
            return ret;
        }
        set 
        {
            this.Entries.Clear();
            for (int i = 0; i < value.Length; i++)
            {
                this.Entries.Add((string)value[i].Key, (KeySafeEntry)value[i].Value);
            }
        }
    }
 
    [XmlArray("Groups")]
    [XmlArrayItem("GroupItem", Type = typeof(DictionaryEntry))]
    public DictionaryEntry[] _x_Group
    {
        get
        {
            DictionaryEntry[] ret = new DictionaryEntry[this.Groups.Count];
            int i = 0;
            foreach (KeyValuePair<string, KeySafeGroup> groupItem in this.Groups)
            {
                DictionaryEntry de = new DictionaryEntry { Key = groupItem.Key, Value = groupItem.Value };
                ret[i] = de;
                i++;
            }
 
            return ret;
        }
        set
        {
            this.Groups.Clear();
            for (int i = 0; i < value.Length; i++)
            {
                this.Groups.Add((string)value[i].Key, (KeySafeGroup)value[i].Value);
            }
        }
    }
}
 
public class KeySafeGroups : Dictionary<string, KeySafeGroup> { }

Open in new window

JAndyEvansAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
If you are getting that exception, then the useful exception is the InnerException, which should give you an exception that explains the reason.
0
JAndyEvansAuthor Commented:
InnerException = {"The type KeySafeEntry was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically."}
0
Bob LearnedCommented:
Are you serializing KeySafeGroups?  If so, it looks like you are missing the Serializable attribute.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

JAndyEvansAuthor Commented:
Like this?  I'm kinda new to serialization.
[Serializable()]
public class KeySafeGroup

Open in new window

0
Bob LearnedCommented:
Like that.
0
JAndyEvansAuthor Commented:
Still get the same problem.
0
Bob LearnedCommented:
Did you read any tutorials, like this?

C# Tutorial - XML Serialization
http://blog.paranoidferret.com/index.php/2008/06/20/csharp-tutorial-xml-serialization/

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.