XML Serialization-driving me nuts!

B

Brian W

I have been going crazy over this for the better part the weekend and have
not found any answers.

I have the class definitions shown below.

The classes Link, LinkCollection, and SiteConfig work perfectly and the
resulting XML looks as one would expect.

The problem is, when I change the definition of the attribute
SiteConfig.PagedLinks to use my other derived collection,
PagedLinkCollection the elements of the collection are serialized to as they
are when I just use LinkCollection. However the attributes in
PagedLinkCollection, MaxItems & More never get serialized. (I also get no
errors serializing & desterilizing)

As an experiment, I MaxItems and More to LinkCollection, they don't get
serialized there either.

Does anyone know how to get this to work?

Thanks
Brian W




[Serializable]
public class SiteConfig
{
public static SiteConfig Instance()
{
SiteConfig settings;
string filePath = @"c:\test.xml";
settings = (SiteConfig)Globals.LoadSerializedObject(typeof(SiteConfig),
filePath);
if ( settings == null )
{
settings = new SiteConfig();
}
return settings;
}
public static void Save(SiteConfig config)
{
Globals.Save(config, @"c:\test.config");
}
private LinkCollection _pagedLinks;
public LinkCollection PagedLinks
{
get { return _pagedLinks; }
set { _pagedLinks = value; }
}
}


[Serializable]
public class PagedLinkCollection : LinkCollection
{
public PagedLinkCollection()
{}
public PagedLinkCollection(PagedLinkCollection value)
{
}
public PagedLinkCollection(Link[] value)
{
}
private int _maxItems = 5;
public int MaxItems
{
get { return _maxItems; }
set { _maxItems = value; }
}
private Link _moreLink;
public Link More
{
get { return _moreLink; }
set { _moreLink = value; }
}
}


[Serializable]
public class LinkCollection : CollectionBase
{

public LinkCollection()
{}
public LinkCollection(LinkCollection value)
{
AddRange(value);
}
public LinkCollection(Link[] value)
{
AddRange(value);
}
public Link this[int index]
{
get { return ((Link)List[index]); }
}
public void AddRange(LinkCollection value)
{
for ( int i = 0; (i < value.Count); i++ )
{
Add((Link)value.List);
}
}

public void AddRange(Link[] value)
{
for ( int i = 0; (i < value.Length); i++ )
{
Add(value);
}
}

public int Add(Link value)
{
return List.Add(value);
}

public bool Contains(Link value)
{
return List.Contains(value);
}

public void CopyTo(Link[] array, int index)
{
List.CopyTo(array, index);
}

public int IndexOf(Link value)
{
return List.IndexOf(value);
}

public void Insert(int index, Link value)
{
List.Insert(index, value);
}

public void Remove(Link value)
{
List.Remove(value);
}

public void RemoveByUrl(string url)
{
foreach (Link link in this)
{
if ( url.ToLower() == link.Url.ToLower() )
{
Remove(link);
return;
}
}
throw new ArgumentOutOfRangeException("URL");
}

public new LinkCollectionEnumerator GetEnumerator()
{
return new LinkCollectionEnumerator(this);
}

public class LinkCollectionEnumerator : IEnumerator
{
private IEnumerator _enumerator;
private IEnumerable _temp;

public LinkCollectionEnumerator(LinkCollection mappings)
{
_temp = ((IEnumerable)(mappings));
_enumerator = _temp.GetEnumerator();
}

public Link Current
{
get { return ((Link)(_enumerator.Current)); }
}

object IEnumerator.Current
{
get { return _enumerator.Current; }
}

public bool MoveNext()
{
return _enumerator.MoveNext();
}

bool IEnumerator.MoveNext()
{
return _enumerator.MoveNext();
}

public void Reset()
{
_enumerator.Reset();
}

void IEnumerator.Reset()
{
_enumerator.Reset();
}
}
}

[Serializable]
public class Link
{
public Link()
{}
private bool _newWindow;
public virtual bool NewWindow
{
get { return _newWindow; }
set { _newWindow = value; }
}
private DateTime _activeDate = DateTime.MinValue;
[XmlAttribute]
public virtual DateTime ActiveDate
{
get { return _activeDate; }
set { _activeDate = value; }
}

private string _url;
public virtual string Url
{
get { return _url; }
set { _url = value; }
}
private string _text;
public virtual string Text
{
get { return _text; }
set { _text = value; }
}
}
 
G

Goran Genter

Brian W said:
I have been going crazy over this for the better part the weekend and
have not found any answers.

I have the class definitions shown below.

The classes Link, LinkCollection, and SiteConfig work perfectly and
the resulting XML looks as one would expect.

The problem is, when I change the definition of the attribute
SiteConfig.PagedLinks to use my other derived collection,
PagedLinkCollection the elements of the collection are serialized to
as they are when I just use LinkCollection. However the attributes in
PagedLinkCollection, MaxItems & More never get serialized. (I also
get no errors serializing & desterilizing)

As an experiment, I MaxItems and More to LinkCollection, they don't
get serialized there either.

Does anyone know how to get this to work?

You must implement ISerializable interface for PagedLinkCollection.
You must instruct CLR how to serialize and deserialize your derived class.

SerializableAttribute isn't enough.
 
B

Brian W

Yep that's what I thought too! So I tried it

I still had no luck, I implemented ISerializable and the constructor that
takes SerializationInfo info & StreamingContext context.

In fact, just for the hell of it, I implemented it on everything, and
GetObjectData and the constructors are never called.

If it makes a difference I am serializing with the XmlSerializer class.

Thanks
Brian W
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top