473,508 Members | 4,324 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XML Serialization-driving me nuts!

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(Si teConfig),
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[i]);
}
}

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

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; }
}
}

Nov 15 '05 #1
2 1099
"Brian W" <brianw@gold_death_2_spam_rush.com> wrote in message
news:#R**************@TK2MSFTNGP09.phx.gbl
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.
Nov 15 '05 #2
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


"Goran Genter" <ge***@fly.srk.fer.hr> wrote in message
news:bi**********@bagan.srce.hr...
"Brian W" <brianw@gold_death_2_spam_rush.com> wrote in message
news:#R**************@TK2MSFTNGP09.phx.gbl
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.

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
4946
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
1
4350
by: andrewcw | last post by:
There is an error in XML document (1, 2). I used XML spy to create the XML and XSD. When I asked to have the XML validated it said it was OK. I used the .net SDK to generate the class. I have...
3
3156
by: Aaron Clamage | last post by:
Hi, I'm not sure that if this is the right forum, but any help would be greatly appreciated. I am porting some java serialization code to c# and I can't figure out the correct way to do it. ...
6
2703
by: Uttam | last post by:
Hello, We are at a very crucial decision making stage to select between .Net and Java. Our requirement is to download a class at runtime on the client computer and execute it using remoting or...
3
2441
by: Alexander | last post by:
When i store rule on PC with .NET.SP1 i cant restore them from PC without SP1. An i get this Error: System.Runtime.Serialization.SerializationException: Possible Version mismatch. Type...
4
11381
by: mijalko | last post by:
Hi, I have inherited my class from System.Drawing.Printing.PrintDocument and I wish to serialize this object using XmlSerializer. And I get exception "There was an error reflecting type ...". If I...
5
6106
by: Nikola Skoric | last post by:
I ran in Mono a program developed on .NET Framework 2.0 and it ran OK until I tried to desirialize a object. There the program died abruptly dumping this: System.ArgumentOutOfRangeException:...
0
6590
by: bharathreddy | last post by:
Before going to that i want to say few thing on serialization : Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an...
1
11044
by: kikisan | last post by:
I am developing a windows service which utilizes the following classes: interface IPersistable; abstract class PersistableObject : IPersistable;
2
5533
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
0
7128
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7058
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5635
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.