Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2007

Tip: Looking for answers? Try searching our database.

Serialize object to XML containing array

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
teddysnips@hotmail.com - 11 May 2007 15:45 GMT
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
Nicholas Paldino [.NET/C# MVP] - 11 May 2007 15:57 GMT
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?

Signature

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

> Windows Application
> Visual Studio 2005
[quoted text clipped - 73 lines]
>
> Edward
sloan - 11 May 2007 16:38 GMT
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/

> Windows Application
> Visual Studio 2005
[quoted text clipped - 73 lines]
>
> Edward

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.