Serialize object to XML containing array

T

teddysnips

Windows Application
Visual Studio 2005

I am new to C# and have done very little XML or serialization.

My client wishes me to store configuration data in an XML file.

It will look something like this:

<definition>
<hardwaredefinitions>
<hardwaredefinition id="1" name="Configuration Data"
editor="ConfigurationPanel">
<hardwarerowdata>
<row value="Channel position 1" />
<row value="Channel position 2" />
<row value="Channel position 3" />
<row value="Channel position 4" />
</hardwarerowdata>
<itemdata>
<gpsstandby value="true" />
<vehiclevoltageusage value="48" />
</itemdata>
</hardwaredefinition>
</hardwaredefinitions>
</definition>

The elements in the <hardwarerowdata> node (<row..../>) will be
displayed in a data grid. The elements in the <itemdata> node
(<gpsstandby etc.) will be used to populate a check box and a text box
on the form.

This data may be changed and saved back to the XML file. I'd like to
use the built-in methods to serialize it, but it barfs because the
<row.../> nodes are being stored in an array and it throws the
following exception:

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

HardwareDefinitionRow is declared thus:

public HardwareDefinitionRow[] HardwareDefinitionRows
{
get { return m_rows; }
}

public class HardwareDefinitionRow
{
public HardwareDefinitionRow(int i)
{
index = i;
}

public int Index
{
get { return index; }
}

public string HardwareDefinition
{
get { return hardwaredefinition; }
set { hardwaredefinition = value; }
}

private int index;
private string hardwaredefinition;

}

HardwareDefinitionRow[] m_rows;

Anyone any idea about how I can crack this?

Thanks

Edward
 
N

Nicholas Paldino [.NET/C# MVP]

Well, the error was pretty obvious. You need a constructor without
parameters on your class in order for it to be serialized.

Why not make the index a property instead and then set it?
 

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