473,396 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Serialize object to XML containing array

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 <hardwarerowdatanode (<row..../>) will be
displayed in a data grid. The elements in the <itemdatanode
(<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

May 11 '07 #1
2 5256
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?

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<te********@hotmail.comwrote in message
news:11**********************@y5g2000hsa.googlegro ups.com...
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 <hardwarerowdatanode (<row..../>) will be
displayed in a data grid. The elements in the <itemdatanode
(<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

May 11 '07 #2

Uhhhh

Define a parameter-less constructor. (as Nicholas pointed out)

See some samples at:
9/21/2005
XmlSerialization with IDictionary and CollectionBase Objects
http://sholliday.spaces.live.com/blog/

<te********@hotmail.comwrote in message
news:11**********************@y5g2000hsa.googlegro ups.com...
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 <hardwarerowdatanode (<row..../>) will be
displayed in a data grid. The elements in the <itemdatanode
(<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

May 11 '07 #3

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

Similar topics

2
by: | last post by:
Hey, I have made a method that would take any object o and an array of extraTypes to construct a XmlSerializer object then call .Serialize(stream, object)... When I have this called within...
7
by: Lars-Erik Aabech | last post by:
Hi! I've got problems with serializing my collections of business objects. The objects themselves serialize fine, but the collections fail. I've got the following structure: Base collection...
14
by: vince | last post by:
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this done...?? thanks, vince
0
by: Ken Durden | last post by:
I'm getting the following exception coming out of a block of unmanaged C++ code. Thread executed for 1.03 sec and died with the following exception: External component has thrown an exception. ...
5
by: Arjen | last post by:
Hello, I have an array "aPeople" with people objects which I serialize like this: XmlSerializer x = new XmlSerializer( typeof(People) ); TextWriter writer = new StreamWriter( "data.xml" );...
6
by: Val | last post by:
How can I serialize/deserialize an object into a string. Existing examples seem to be showing this operation for files only. Thansk
2
by: Joe | last post by:
Hi I have a Generics List in a PropertyGrid I am able to Serialize it to XML but when I try to deserialize back to the class of the PropertyGrid The Constructor doesn't seem to fire to reload...
7
by: Sugandh Jain | last post by:
I am using a method to serialize objects to xml. this method is working fine to serialize most of the objects to Xml. For one collection object, it is not doing so. The Collection and its...
3
by: Andrew Robinson | last post by:
I am using the folllowing code to serialize a object to a byte array (byte). It works fine, but my resulting array is size is being rounded up to the next largest 32768 boundary. How can I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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
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,...

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.