【问题标题】:C# deserialize xml get element valueC#反序列化xml获取元素值
【发布时间】: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


    【解决方案1】:

    使用[XmlText] 属性:

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

    【讨论】:

      【解决方案2】:

      尝试使用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; }
      }
      
      }
      

      【讨论】:

      • 我更喜欢干净的代码而不是生成的代码。 mm8 为我提供了解决方案。但是谢谢你和我一起思考。 Windows 也有一个从 xml 自动生成 xsd 的功能,如果使用 xsd.exe,您可以生成一个 c# 文件。
      • 没问题。这就是为什么我说您可以在使用代码之前删除所有不需要的属性,或者仅将其用作调试参考。
      • 啊,我也可以这样做。我没想到。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-20
      • 2019-10-03
      相关资源
      最近更新 更多