【发布时间】:2020-05-08 23:57:31
【问题描述】:
我正在尝试反序列化由其他软件创建的文件。我似乎无法弄清楚元素名称具有 2 个属性并且本身具有值的部分。
任何帮助将不胜感激:
public class Channel
{
[XmlAttribute("channelNumber")]
public string channelNumber;
[XmlAttribute("status")]
public string status;
[XmlAttribute("type")]
public string type;
[XmlAttribute("ca")]
public string ca;
[XmlAttribute("shortName")]
public string shortName;
[XmlAttribute("outOfBand")]
public string outOfBand;
//[XmlElement]
//[XmlAnyElement("Name")]
//[XmlAnyElement]
[XmlElement("Name")]
public NameClass Name;
}
//[XmlRoot(ElementName = "Name")]
public class NameClass
{
//[XmlElement(Order = 1)]
//[XmlAttribute]
[XmlAttribute("lang")]
public string lang { get; set; }
//[XmlIgnore]
//[XmlElement(Order = 2)]
//[XmlAttribute("xmlns")]
//[XmlAttribute]
//public string xmlns;
//[XmlElement("Name")]
[XmlText]
public string Value { get; set; }
}
我把所有我尝试过的东西都留在了……下面是 XML 文件的一部分:
<Channel channelNumber="1" status="active" type="dt" ca="false" shortName="CH" outOfBand="true">
<Name lang="eng" xmlns="http://www.atsc.org/XMLSchemas/pmcp/2007/3.1">CH</Name>
</Channel>
<ScheduleName>2020-05-06 CH Log</ScheduleName>
我看不懂的部分是从名称“CH”和名称的属性(“lang”和“xmlns”)获取值,它们总是为空?
【问题讨论】:
-
这是一个不同的命名空间。来自:[XmlElement("Name")] 到:[XmlElement(ElementName = "Name", Namespace = "atsc.org/XMLSchemas/pmcp/2007/3.1")]
标签: c# xml namespaces attributes deserialization