本文介绍了XML字符串反序列化到C#对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到的XML文件是这样的。
I receive xml file like this.
<radio>
<channel id="Opus">
<display-name>Opus</display-name>
<icon src="" />
</channel>
<channel id="Klasika">
<display-name>Klasika</display-name>
<icon src="" />
</channel>
<channel id="LR">
<display-name>LR</display-name>
<icon src="" />
</channel>
<programme channel="Opus" start="20130203060000" stop="20130203110000" duration="050000">
<title lang="lt">OPUS muzika.</title>
<desc lang="lt">OPUS muzika.</desc>
<category lang="lt">muzikos laidos</category>
<date>2013.02.03</date>
</programme>
<programme channel="Opus" start="20130203110000" stop="20130203150000" duration="040000">
<title lang="lt">Vėlyvi pusryčiai su OPUS.</title>
<desc lang="lt">Vėlyvi pusryčiai su OPUS.</desc>
<category lang="lt">muzikos laidos</category>
<date>2013.02.03</date>
</programme>
</radio>
是程序和频道的多个实例。我尝试将其反序列化到这个C#的对象,但我得到一个空,而不是对象的:
with many instances of programme and channel. I try to deserialize it into this c# object but I get a null instead of object:
[XmlRoot("radio")]
public sealed class radio
{
[XmlRoot("channel")]
public sealed class channel
{
[XmlAttribute("id")]
public string id { get; set; }
[XmlElement("display-name")]
public string displayName { get; set; }
[XmlElement("icon")]
public string icon { get; set; }
public channel()
{
}
}
[XmlRoot("programme")]
public sealed class programme
{
[XmlAttribute("channel")]
public string channel { get; set; }
[XmlAttribute("start")]
public string start { get; set; }
[XmlAttribute("stop")]
public string stop { get; set; }
[XmlAttribute("duration")]
public string duration { get; set; }
[XmlElement("title")]
public string title { get; set; }
[XmlElement("desc")]
public string desc { get; set; }
[XmlElement("category")]
public string category { get; set; }
[XmlElement("date")]
public string date { get; set; }
public programme()
{
}
}
[XmlArray]
public channel[] channels { get; set; }
[XmlArray]
public programme[] programmes { get; set; }
public radio()
{
channels = null;
programmes = null;
}
public static radio FromXmlString(string xmlString)
{
var reader = new StringReader(xmlString);
var serializer = new XmlSerializer(typeof(radio));
var instance = (radio)serializer.Deserialize(reader);
return instance;
}
}
我在做什么错的,什么是正确的XML对象类?
What am I doing wrong and what would be the proper xml object class?
推荐答案
您将只需要改变你的无线
类了一下,因为2对象类型的混合相同的数组,你将不得不增加一些属性,让串行知道什么是什么。
You will just have to change you Radio
class a bit, since the 2 object types a mixed in the same array you will have to add some attributes to let the serializer know whats what.
[XmlRoot("radio")]
public sealed class radio
{
[XmlElement("channel", Type = typeof(channel))]
public channel[] channels { get; set; }
[XmlElement("programme", Type = typeof(programme))]
public programme[] programmes { get; set; }
public radio()
{
channels = null;
programmes = null;
}
public static radio FromXmlString(string xmlString)
{
var reader = new StringReader(xmlString);
var serializer = new XmlSerializer(typeof(radio));
var instance = (radio)serializer.Deserialize(reader);
return instance;
}
}
[Serializable]
public class channel
{
[XmlAttribute("id")]
public string id { get; set; }
[XmlElement("display-name")]
public string displayName { get; set; }
[XmlElement("icon")]
public string icon { get; set; }
public channel()
{
}
}
[Serializable]
public sealed class programme
{
[XmlAttribute("channel")]
public string channel { get; set; }
[XmlAttribute("start")]
public string start { get; set; }
[XmlAttribute("stop")]
public string stop { get; set; }
[XmlAttribute("duration")]
public string duration { get; set; }
[XmlElement("title")]
public string title { get; set; }
[XmlElement("desc")]
public string desc { get; set; }
[XmlElement("category")]
public string category { get; set; }
[XmlElement("date")]
public string date { get; set; }
public programme()
{
}
}
结果:
这篇关于XML字符串反序列化到C#对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!