• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3893
  • Last Modified:

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

0
JAndyEvans
Asked:
JAndyEvans
  • 4
  • 3
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now