471,122 Members | 845 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,122 software developers and data experts.

IXmlSerializable and custom collections

Situation:
---------------------------
I have an 'export'-wrapper to my regular objects. For each regular object
there is also an export object. An export object derives from the regular
object and has for each property in the base object an extra boolean
property. That extra properties tells wether to include the base property in
the final export (= webservice) or not. That is because not all the
properties may be exposed all the time (depending on who is requesting). To
implement this I have used the IXmlSerializable interface on the export
objects. I also used the IXmlSerializable interface so I can choose the xml
element names, especially on the collections (so I don't get ArrayOf.....).
The webservice returns export collection objects (implementing
IXmlSerializable).

Problem:
---------------------------
To test my webservice I have created a simple WinForms application which
calls one of the webmethods. But when adding the Web Reference to my test
application the proxy is not well generated. Instead of returning my custom
collection types it returns a DataSet. I can work around that issue if I only
return a single object instead of a collection: I just changed the return
type in the generated proxy to my custom collection type. But then I needed
to add an extra reference to my Export assambly.

I've tried the same thing to return my custom collection, but that does not
work unless I do not implement the IXmlSerializable interface on the
collection. If I do that, I loose the ability to choose a custom xml element
name. When I implement the IXmlSerializable interface I get this error:
An unhandled exception of type 'System.InvalidOperationException' occurred
in system.web.services.dll
Additional information: Method Export.GetTest can not be reflected.

Question:
---------------------------
Why do I get this somewhat strange error? I suppose the framework does not
know the TestExport object. If this is correct, why does the framework know
if I don't implement IXmlSerializable. Is there anyone who can help me out
here?

You can find a stripped code example of my situation below. My apologies for
this long post and thanks in advance!
Best regards,
Thomas

My code:
---------------------------
I have stripped down the code here for simplicity. My export class does not
derive from any base class, nor does it have the booleans. However when I try
the code below I still have the same problem.
// this is the export object which normally derives from 'Test'.
[Serializable]
public class TestExport : IXmlSerializable
{
private string name;
private string email;

public TestExport()
{
}

public string Name
{
get { return name; }
set { name = value; }
}

public string Email
{
get { return email; }
set { email = value; }
}

#region IXmlSerializable
public XmlSchema GetSchema()
{
return null;
}

public void ReadXml(XmlReader reader)
{
// Read the opening tag of the encapsulating element
reader.ReadStartElement();

// Open the root element for our object
reader.ReadStartElement("Test");

reader.ReadStartElement("Name");
this.Name = reader.ReadString();
reader.ReadEndElement();

reader.ReadStartElement("Email");
this.Email = reader.ReadString();
reader.ReadEndElement();

// Close the root element for our object
reader.ReadEndElement();

// Read the end tag of the encapsulating element
reader.ReadEndElement();
}

public void WriteXml(XmlWriter writer)
{
writer.WriteStartElement("Test");
writer.WriteElementString("Name", this.Name);
writer.WriteElementString("Email", this.Email);
writer.WriteEndElement();
}
#endregion
}
[Serializable]
public class TestenExport : CollectionBase, IXmlSerializable
{
public TestenExport()
{
}

#region IXmlSerializable
public XmlSchema GetSchema()
{
return null;
}

public void ReadXml(XmlReader reader)
{
// Read the opening tag of the encapsulating element
reader.ReadStartElement();

// Open the root element for our object
reader.ReadStartElement("Tests");

foreach(TestExport item in this)
{
item.ReadXml(reader);
}

// Close the root element for our object
reader.ReadEndElement();

// Read the end tag of the encapsulating element
reader.ReadEndElement();
}

public void WriteXml(XmlWriter writer)
{
writer.WriteStartElement("Tests");
foreach(TestExport item in this)
{
item.WriteXml(writer);
}
writer.WriteEndElement();
}
#endregion

#region CollectionBase
public int Add(TestExport @value)
{
return List.Add(@value);
}

public TestExport this[int index]
{
get
{
return (TestExport)(List[index]);
}
set
{
List[index] = value;
}
}
#endregion
}

Nov 12 '05 #1
0 1754

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by theburnetts | last post: by
9 posts views Thread by Greger | last post: by
reply views Thread by Rajesh | last post: by
2 posts views Thread by =?Utf-8?B?Ym9iYnk=?= | last post: by

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.