views:

529

answers:

2

I've got this class being provided by a web service that is then being consumed by a Silverlight app (I don't know if that's relevant or not)

[Serializable]
public class Entry
{
 private string _title;

 public string Id { get; set; }
 public string Title { get { return _title; } set { _title = value; } }
 public string Link { get; set; }
 public DateTime Published { get; set; }
 public DateTime Updated { get; set; }
 public User User { get; set; }
 public Service Service { get; set; }
 public List<Comment> Comments { get; set; }
 public List<Like> Likes { get; set; }
 public List<Media> Media { get; set; }
}

The _title variable I added to demonstrate what's going wrong. When I reference the web service in my silverlight app, it generates the folowing xsd:

  <xs:complexType name="Entry">
    <xs:sequence>
      <xs:element name="_title" nillable="true" type="xs:string" />
      <xs:element name="_x003C_Comments_x003E_k__BackingField" nillable="true" type="tns:ArrayOfComment" />
      <xs:element name="_x003C_Id_x003E_k__BackingField" nillable="true" type="xs:string" />
      <xs:element name="_x003C_Likes_x003E_k__BackingField" nillable="true" type="tns:ArrayOfLike" />
      <xs:element name="_x003C_Link_x003E_k__BackingField" nillable="true" type="xs:string" />
      <xs:element name="_x003C_Media_x003E_k__BackingField" nillable="true" type="tns:ArrayOfMedia" />
      <xs:element name="_x003C_Published_x003E_k__BackingField" type="xs:dateTime" />
      <xs:element name="_x003C_Service_x003E_k__BackingField" nillable="true" type="tns:Service" />
      <xs:element name="_x003C_Updated_x003E_k__BackingField" type="xs:dateTime" />
      <xs:element name="_x003C_User_x003E_k__BackingField" nillable="true" type="tns:User" />
    </xs:sequence>
  </xs:complexType>

Note only the title property is simply named, the others are named <Link>_BackingField which completely dies when you try and load the element because you can't have < or > in the name of a property.

Why is it serializing the backing fields and not the public properties?

+1  A: 

Don't use automatic properties. Instead of writing:

public string Id { get; set; }

write:

string id;
public string Id { get { return id;} set {id = value;} }

In case of Automatic Properties only backing field gets serialized and that's why you get strange names.

jarek
+3  A: 

As explained in this article, when you use the DataContractSerializer (the default serializer for WCF) in combination with the Serializable attribute, the behavior is that all fields, public and private, are serialized. Because the backing fields are auto-generated in your case, the compiler comes up with funny names that have no chance of conflicting with any field names you might create (C# may not accept "<" or ">" in identifiers, but the CLR is not so picky).

Probably the easiest way to rectify this situation is to add DataContract and DataMember attributes to the Entry class as appropriate.

Daniel Pratt