0

All,

I have an instance of ProjectBudget class returned from a web method.

Ex:

[WebMethod()]
    public ProjectBudget LoadBudget(int id)
    {
       ProjectBudget  budget = BudgetManager.LoadBudget(id);
        return budget;
    }

The ProjectBudget class contains the following defintion:

 public class ProjectBudget
{
    public int Id = -1;
    public long VersionNumber = -1;
    public string QuoteNumber = "";
    public string CurrencyCode = "";
    public ProjectInfo Project;
    public ClientInfo Client;

    public readonly List<InventoryItem> Inventory = new List<InventoryItem>();
    public readonly List<Staff> Staff = new List<Staff>();
    public readonly List<CodeType> Departments = new List<CodeType>();
    public readonly SerializableDictionary<string, string> Tasks = new SerializableDictionary<string, string>();

    public ProjectBudget()
    {
    }

}

All public fields you see are serialized just fine with the exception of Tasks field, which is completely ignored by XML serializer. Since we all know by now that Dictionaries cannot be handled by XML serializer, I use a serializable dictionary (which is just a dictionary that implements IXmlSerializable) here but XML serializer decides to ignore it completely, i.e. the XML output does not contain any tasks and the generated proxy class doesn't have this field.

I need to figure out how to tell the XML serializer not to omit this field.

Btw, what is interesting is that a web method that returns SerializableDictionary works fine!

ActiveX
  • 1,064
  • 1
  • 17
  • 37

1 Answers1

0

A very similar question as yours appears to have been asked already: Link.

Use DataContractSerializer or try explicitly implementing your getter (and setter), as per this link.

Community
  • 1
  • 1
Euric
  • 339
  • 2
  • 6
  • Thx for your response. Yes, the link to the above post saved me although the guy on that post dealt with a bit different issue. It turns out that using the readonly keyword on my dictionary caused the XML serializer to ignore the field. Btw, adding a property and keeping the readonly keyword will not work! You need to drop the readonly keyword on the custom dictionary regardless wheter you use properties or fields. – ActiveX Aug 14 '12 at 22:15
  • I would like to add, it is odd it works on the other fields (with readonly keyword as you can see from my example) but when it comes to the custom type it choses to ignore it, without any error! – ActiveX Aug 14 '12 at 22:20
  • Glad I could help! I too was surprised it worked for the other readonly properties. I've always worked from the premise that readonly properties are never serialized. – Euric Aug 14 '12 at 22:34