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

带有自定义类型成员的C#Xml反序列化,c#,xml-deserialization,custom-type,C#,Xml Deserialization,Custom Type,我有一个要反序列化的对象,但该对象具有无法序列化的自定义类型ApplicationLanguage [Serializable] public class FieldTranslation { // how is this possible? // does the 'en-us' in this member is reachable in this concept? //public ApplicationLanguage L

我有一个要反序列化的对象,但该对象具有无法序列化的自定义类型
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; }
    }
我构建了一个API来从缓存中获取type
ApplicationLanguage
,如下所示:

ApplicationLanguage  en= ApplicationLanguagesList.Get("en-us");
我是否可以在上面的序列化中组合自定义类型

这是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>

请提供您的{0}
{0}无效
名字

澄清:此答案基于预编辑xml,其中存在

<Language>he</Language>

这里,
LangSerialized
成员用作
Lang
缩写形式的代理。使用
XmlSerializer
时,要求该成员是
public
,但我添加了一些其他属性,以使其从大多数其他常见用法中消失。

您可以像下面这样手动填充集合中的字段:

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

您还可以在应用程序语言类中实现
IXmlSerializable
接口

[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; }
}

然后将language属性设置为serialize

谢谢,您能再次查看我的xml并告诉我如何导航到'lang=“en-us”`thanks@SexyMF啊,对,;我错误地认为您指的是
元素。不,您不能从
FieldTranslation
类型执行此操作-您需要像Scott这样的包装器illustrates@SexyMF(重新接受的答案)我的理解是,斯科特的答案更准确地反映了你的情况……注意;这里的
[Serializable]
没有任何作用
[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; }
}