Stu*_*urf 2 c# xml deserialization
我试图从翻译中获取 XmlElement 的值。
当我调试代码时,该值为空。Translations.Section.Translation我正在尝试获取in的值Translation.cs。我似乎无法找出我做错了什么以及为什么。谁能向我解释我需要做什么?
我已经走到这里了,但我不知道如何填充该值。
<?xml version="1.0" encoding="utf-8"?>
<Translations code="nl" description="Dutch" xmlns="urn:Test.Translations">
<Section name="Module">
<Translation key="SystemConfiguration">Systeem configuratie</Translation>
</Section>
<Section name="Feature">
<Translation key="Feature">Feature</Translation>
<Translation key="Name">Naam</Translation>
<Translation key="IsEnabled">Actief</Translation>
</Section>
</Translations>
Run Code Online (Sandbox Code Playgroud)
[Serializable()]
[XmlRoot(Namespace = "urn:Test.Translations", ElementName = "Translations", DataType = "string", IsNullable = true)]
public class Translations
{
[XmlAttribute("code")]
public string Code { get; set; }
[XmlAttribute("description")]
public string Description { get; set; }
[XmlElement("Section")]
public List<Section> Sections { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
public class Section
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlElement("Translation")]
public List<Translation> Translations { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
public class Translation
{
[XmlAttribute("key")]
public string Key { get; set; }
//TODO Get value (This is null)
[XmlElement("Translation")]
public string Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
var serializer = new XmlSerializer(typeof(Translations), "");
using (var reader = new StreamReader(xmlFilePath))
{
var translationFile = (Translations) serializer.Deserialize(reader);
reader.Close();
}
Run Code Online (Sandbox Code Playgroud)
使用[XmlText]属性:
public class Translation
{
[XmlAttribute("key")]
public string Key { get; set; }
[XmlText]
public string Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)