.NET Web Service Silverlight Serialization Throws XML Error

I have this class provided by a web service which is then consumed by a Silverlight application (I don't know if this is consistent 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; }
}

      

Variable _title

I added to demonstrate what is going wrong. When I reference the web service in my silverlight application, it generates the following 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 that the property name title is just called, others are called <Link>_BackingField

, which completely dies when you try to load the element, because you cannot have <or> in the property name.

Why does this serialize backing fields and not public properties?

0


source to share


2 answers


As explained in this article , when you use DataContractSerializer

(WCF's default serializer) in combination with Serializable

an attribute, the behavior is that all fields, public and private, are serialized. Since the backing fields are automatically generated in your case, the compiler has funny names that have no way of contradicting any field names you might create (C # may not accept "<" or ">" in identifiers, but the CLR is not that legible).



Probably the easiest way to remedy this situation - to add attributes DataContract

and DataMember

in the class Entry

, if necessary.

+3


source


Don't use automatic properties. Instead of writing:

public string Id { get; set; }

      

records:



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

      

In the case of automatic properties, only the backing field gets serialized and that's why you get weird names.

+1


source







All Articles