C#反序列化XML获取元素值

时间:2019-06-28 09:08:46

标签: c# xml deserialization

我正在尝试从Translation中获取XmlElement的值。

当我调试代码时,该值为null。我正在尝试获取Translations.Section.TranslationTranslation.cs的值。我似乎无法找出我在做什么以及为什么做错了。谁能向我解释我需要做什么?

我走了这么远,但我不知道如何填补价值。

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();
}

2 个答案:

答案 0 :(得分:3)

使用[XmlText]属性:

public class Translation
{
    [XmlAttribute("key")]
    public string Key { get; set; }

    [XmlText]
    public string Value { get; set; }
}

答案 1 :(得分:1)

尝试使用XML2CSharp生成类,然后重试,看看是否仍然为空。

XML的生成代码如下所示: (您可以删除不需要的属性)

/* 
Licensed under the Apache License, Version 2.0

http://www.apache.org/licenses/LICENSE-2.0
*/
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
[XmlRoot(ElementName="Translation", Namespace="urn:Test.Translations")]
public class Translation {
    [XmlAttribute(AttributeName="key")]
    public string Key { get; set; }
    [XmlText]
    public string Text { get; set; }
}

[XmlRoot(ElementName="Section", Namespace="urn:Test.Translations")]
public class Section {
    [XmlElement(ElementName="Translation", Namespace="urn:Test.Translations")]
    public List<Translation> Translation { get; set; }
    [XmlAttribute(AttributeName="name")]
    public string Name { get; set; }
}

[XmlRoot(ElementName="Translations", Namespace="urn:Test.Translations")]
public class Translations {
    [XmlElement(ElementName="Section", Namespace="urn:Test.Translations")]
    public List<Section> Section { get; set; }
    [XmlAttribute(AttributeName="code")]
    public string Code { get; set; }
    [XmlAttribute(AttributeName="description")]
    public string Description { get; set; }
    [XmlAttribute(AttributeName="xmlns")]
    public string Xmlns { get; set; }
}

}