XmlSerializer List<T>

C

Caspian

can anyone help with this problem, it seems to fail when I try to
initialise the XMLSerializer.

I'm getting a "There was an error reflecting type ... "

innerexception: "Cannot serialize member
'CommercialWebServices.DataAccess.ResponseStructures.RequestParameters.Parameter'
of type 'System.Collections.Generic.List`

I'm trying to serialize the following ...

[Serializable]
public class RequestParameters
{

[XmlArray("Parameter")]
public List<oParameter> Parameter = new List<oParameter>();

public RequestParameters()
{
}
}

[Serializable]
public class oParameter
{
private string Parameter_ArgumentName;
private string Parameter_ArgumentValue;

[XmlAttribute("Name")]
public string Name
{
get { return Parameter_ArgumentName; }
set { Parameter_ArgumentName = value; }
}

[XmlAttribute("Value")]
public string Value
{

get { return Parameter_ArgumentValue; }
set { Parameter_ArgumentValue = value; }

}

public oParameter(string name, string value)
{
Parameter_ArgumentName = name;
Parameter_ArgumentValue = value;
}
}

.................................
I'm then trying to serialize the data with the following ...

try
{

RequestParameters testrequest = new
RequestParameters();

testrequest.Parameter.Add(new oParameter("Artist",
"Bob Marley"));
testrequest.Parameter.Add(new oParameter("Title", "No
Woman"));

XmlSerializer ser = new
XmlSerializer(typeof(RequestParameters));
XmlTextWriter writer = new XmlTextWriter("c:\
\RequestParameters.xml", System.Text.Encoding.UTF8);

}
catch (Exception ee)
{
Console.WriteLine(ee.Message);
}


Any help greatfully received,

Regards,

Tim
 
M

Marc Gravell

Check the inner exceptions:

?ee.InnerException.InnerException.Message

"oParameter cannot be serialized because it does not have a
parameterless constructor."

Add one and you're done
 

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