Reputation: 11185
I have an object to deserialize but the object has custom type ApplicationLanguage
that cannot be serialize.
[Serializable]
public class FieldTranslation
{
// how is this possible?
// does the 'en-us' in this member is reachable in this concept?
//public ApplicationLanguage Lang = ApplicationLanguagesList.Get("en-us");
//public ApplicationLanguage Lang { get; set; }
[XmlAttribute("name")]
public string Name{ get; set; }
public string Tooltip { get; set; }
public string Label { get; set; }
public string Error { get; set; }
public string Language { get; set; }
}
I built an API to get type ApplicationLanguage
from the cache like that:
ApplicationLanguage en= ApplicationLanguagesList.Get("en-us");
Is there anyway that I can combine the custom type in the serialization above?
this is the xml:
<Fields lang="en-us">
<Item name="FirstName">
<Tooltip>Please provide your {0}</Tooltip>
<Error>{0} Is not valid</Error>
<Label>First Name</Label>
</Item>
</Fields>
Upvotes: 2
Views: 1710
Reputation: 1063704
Clarification: this answer is based on the pre-edit xml, where there was an
<Language>he</Language>
element. This answer does not apply to the <Fields lang="en-us">
scenario.
Something like:
[XmlIgnore]
public ApplicationLanguage Lang { get; set; }
[XmlElement("Language")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public string LangSerialized {
get { return Lang == null ? null : Lang.Name; } // or where-ever "he" comes from
set { Lang = value == null ? null : ApplicationLanguagesList.GetByName(value); }
}
Here the LangSerialized
member is used as a proxy to the short-form of Lang
. With XmlSerializer
it is required that this member is public
, but I've added a few other attributes to make it disappear from most other common usages.
Upvotes: 2
Reputation: 4294
you could change your class struct like
[Serializable]
[XmlRoot("Fields")]
public class FieldCollection
{
[XmlAttribute("lang")]
public string Lanuage { get; set; }
[XmlElement("Item")]
public FieldTranslation[] Fields { get; set; }
}
[Serializable]
public class FieldTranslation
{
[XmlAttribute("name")]
public string Name { get; set; }
public string Tooltip { get; set; }
public string Label { get; set; }
public string Error { get; set; }
public string Language { get; set; }
}
and then set the language property to serialize
Upvotes: 2
Reputation: 465
You also could implement a IXmlSerializable
interface in your ApplicationLanguage class
Upvotes: 0
Reputation: 2733
You can manually fill a field in the collection above like this:
[XmlAttribute("lang")]
public string ManuallyFilledLang{ get; set; }
Upvotes: 0