【发布时间】:2019-06-28 09:08:46
【问题描述】:
我正在尝试从翻译中获取 XmlElement 的值。
当我调试代码时,该值为空。我试图在Translation.cs 中获取Translations.Section.Translation 的值。我似乎无法找出我做错了什么以及为什么。谁能向我解释我需要做什么?
我走了这么远,但我不知道如何填写价值。
XML 文档
<?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>
Translations.cs
[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; }
}
Section.cs
public class Section
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlElement("Translation")]
public List<Translation> Translations { get; set; }
}
Translation.cs
public class Translation
{
[XmlAttribute("key")]
public string Key { get; set; }
//TODO Get value (This is null)
[XmlElement("Translation")]
public string Value { get; set; }
}
program.cs
var serializer = new XmlSerializer(typeof(Translations), "");
using (var reader = new StreamReader(xmlFilePath))
{
var translationFile = (Translations) serializer.Deserialize(reader);
reader.Close();
}
【问题讨论】:
标签: c# xml deserialization