views:

235

answers:

2

I've got a class with a version (which actually contains the Version class) property. Like I said, version contains an instance of Version, which looks like this:

  • Version
    • Build: 111
    • Major: 1
    • MajorRevision: 0
    • Minor: 1
    • MinorRevision: 10
    • Revision: 10

When I serialize the class, version is always empty:

<Version />

The Client-class looks like:

 [Serializable]
    public class Client
    {
        public string Description;

        public string Directory;

        public DateTime ReleaseDate;

        public Version Version;
    }
+2  A: 

System.Version is not serializable, if you look at it's properties on MSDN, you'll see they have no setters...so the serializer won't store them. However, this approach still works. That article (old but still works) provides a Version class that is serializable, can you switch to that and get going?

Nick Craver
that class helped me out! thanks!
MysticEarth
+1  A: 

You need to define your get and set accessors, as:

public class Version
{
    public int Build { get; set; }
    public int Major { get; set; }
    public int MajorRevision { get; set; }
    public int Minor { get; set; }
    public int MinorRevision { get; set; }
    public int Revision { get; set; }
}

// ...

new XmlSerializer(typeof (Version))
    .Serialize(Console.Out,
               new Version()
                   {
                       Build = 111,
                       Major = 1,
                       MajorRevision = 0,
                       Minor = 1,
                       MinorRevision = 10,
                       Revision = 10
                   }
    );

I got this output:

<?xml version="1.0" encoding="ibm850"?>
<Version xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  <Build>111</Build>
  <Major>1</Major>
  <MajorRevision>0</MajorRevision>
  <Minor>1</Minor>
  <MinorRevision>10</MinorRevision>
  <Revision>10</Revision>
</Version>
Rubens Farias
Work too, but i used the class Nick Craver suggested. Thanks!!!
MysticEarth
Sorry, I didn't noticed your're using `System.Version`
Rubens Farias