1

This is the class I'm trying to serialize

[Serializable]
public class PendingAccountInfo 
{
        public AccountId AccountId { get; set; }
        public string EmailAddress { get; set; }
}

[Serializable]
public struct AccountId : IEquatable<AccountId> 
{
    private readonly int _id;

    public AccountId(int id) {
        _id = id;
    }

    public int Id {
        get { return _id; }
    }
    ...
}

This is how I do the serialization

XmlSerializer xmlserializer = new XmlSerializer(typeof(List<T>));
StringWriter stringWriter = new StringWriter();

XmlWriterSettings settings = new XmlWriterSettings { OmitXmlDeclaration = true, Indent = true };

XmlWriter writer = XmlWriter.Create(stringWriter, settings);

xmlserializer.Serialize(writer, value);

string result = stringWriter.ToString();

This is what I get

<PendingAccountInfo>
  <AccountId />
  <EmailAddress>test@test.com</EmailAddress>
</PendingAccountInfo>

From what I read, this should work, but I must be missing something

ThunderDev
  • 1,118
  • 2
  • 19
  • 38
  • 2
    Write a public getter/setter for property `Id`. An empty contructor may also be needed... – L.B Aug 11 '14 at 13:15

1 Answers1

3

The problem here comes from your readonly property. As explained in this other thread, XmlSerializer only serialize property with get/set accessibility.

What you can do is either make your property settable or change your serializer.

Community
  • 1
  • 1
timothy.B
  • 116
  • 6