views:

106

answers:

1

Here's my class:

[Serializable()]
    [XmlRootAttribute("Language")]
    public class Language : ISerializable
    {
     string Id {
      get;
      set;
     }
     string Part2B {
      get;
      set;
     }
     string Part2T {
      get;
      set;
     }
     string Part1 {
      get;
      set;
     }
     string Scope {
      get;
      set;
     }
     string LanguageType {
      get;
      set;
     }
     string RefName {
      get;
      set;
     }
     string Comment {
      get;
      set;
     }

snipped

I'm returning an array of them from a Mono web service, like this:

[WebMethod()]
     [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
     public Language[] GetLanguages()
     {
      List<Language> languages;
      languages = GetLanguageList();
      return languages.ToArray();
     }

But what I get is this:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfLanguage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/"&gt;
  <Language />
  <Language />
  <Language />
  <Language />
</ArrayOfLanguage>

Why are the members not getting serialized?

+6  A: 

Your properties are not public.
XmlSerialization only serialises public fields and properties.

XML serialization is the process of converting an object's public properties and fields to a serial format

The default accessibility for fields and properties (indeed all members) is private in c#.

Also implementing ISerializable has no effect on XmlSerialization (that would be IXmlSerializable).
Neither does the [Serializable] attribute, instead you need one or more of these.

ShuggyCoUk
Duh! That'll teach me to code under the influence :)
Chris McCall
:) It's a horrid mix to a hangover
ShuggyCoUk
Nor does `[Serializable]` ;-p
Marc Gravell
I was throwing the kitchen sink at it to get it to work :) Thanks, gang, you're lifesavers :)
Chris McCall
I miseed that too, thanks Marc, integrated
ShuggyCoUk