简体   繁体   English

具有自定义类型成员的C#Xml反序列化

[英]C# Xml deserialization with custom type member

I have an object to deserialize but the object has custom type ApplicationLanguage that cannot be serialize. 我有一个要反序列化的对象,但是该对象具有无法序列化的自定义类型ApplicationLanguage

 [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: 我构建了一个API,以从缓存中获取ApplicationLanguage类型,如下所示:

ApplicationLanguage  en= ApplicationLanguagesList.Get("en-us");

Is there anyway that I can combine the custom type in the serialization above? 无论如何,我可以在上面的序列化中组合自定义类型吗?

this is the xml: 这是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>

Clarification: this answer is based on the pre-edit xml, where there was an 澄清:此答案基于预编辑xml,其中存在一个

<Language>he</Language>

element. 元件。 This answer does not apply to the <Fields lang="en-us"> scenario. 这个答案并不适用于<Fields lang="en-us">情景。

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 . 在这里, LangSerialized成员用作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. 使用XmlSerializer ,要求该成员是public ,但是我添加了一些其他属性,以使其从大多数其他常用用法中消失。

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 然后将language属性设置为序列化

You can manually fill a field in the collection above like this: 您可以像这样在上面的集合中手动填写一个字段:

    [XmlAttribute("lang")]
    public string ManuallyFilledLang{ get; set; }

您还可以在ApplicationLanguage类中实现IXmlSerializable接口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM