【发布时间】:2020-03-28 12:38:18
【问题描述】:
我是 C# 的新手,所以我希望你们能在这里指导我。我有以下 XML,应该将其转换为带有代码、描述和语言的对象,但我真的很难获得 XML 的“语言”属性值。
目前它只返回'code'和'description'的值,而'lang'返回null。
<?xml version="1.0" encoding="UTF-8"?>
<statisticgroup>
<code>2049</code>
<description lang="en-GB">2049</description>
</statisticgroup>
[Serializable]
public class XmlStatisticsModel
{
[XmlElement ( "code" )]
public string Code { get; set; }
[XmlElement ( "description" )]
public string Description { get; set; }
[XmlAttribute ( "lang" )]
public string Lang { get; set; }
}
【问题讨论】:
-
代码无法运行。类名和标签名必须相同或使用[XmlRoot("statisticgroup")]
-
您无法通过这种方式读取语言属性,因为它不是
statisticgroup节点(代表您的类)的属性,而是<Description>node 的属性。将描述更改为一个类并在那里指定属性。 -
是的,@OguzOzgul 是对的。
Description在这里是一个 ComplexType,而不是一个原始类型 -
我的回答还包含使用
[XmlText]属性读取描述值 2049。 -
opps.. 我的意思是.. 它是 ComplexType,而不是 SimpleType.. 从某种意义上说,它可能是一个原始类型,它不是从任何东西派生的..
标签: c# xml deserialization