gpt4 book ai didi

c# - 无法反序列化 xml 文件

转载 作者:太空宇宙 更新时间:2023-11-03 22:01:11 24 4
gpt4 key购买 nike

我有一个像下面这样的 xml 文件,我正在尝试反序列化。

<info>
<result resultCode="0000"><![CDATA[操作成功]]></result>
<songlist>
<song>
<id>63672</id>
<name><![CDATA[红玫瑰]]></name>
<singer id="1620"><![CDATA[陈奕迅]]></singer>
<album id="22056"><![CDATA[认了吧]]></album>
<remark><![CDATA[]]></remark>
<uploadinguser><![CDATA[]]></uploadinguser>
<collectinguser><![CDATA[]]></collectinguser>
<source>
<link id="3441591" filesize="3842715" format="mp3"><![CDATA[http://space6.j.cn/olympic/edit/672/63672-3842715.mp3]]></link>
<link id="3435011" filesize="3843133" format="mp3"><![CDATA[http://f8.wretch.yimg.com/satyedhome/32764/1165646407.mp3]]></link>
<link id="3434519" filesize="3842715" format="mp3"><![CDATA[http://space0.j.cn/olympic/edit/672/63672-3842715.mp3]]></link>
</source>
</song>
<song>
<id>67228</id>
<name><![CDATA[光荣]]></name>
<singer id="106"><![CDATA[BOBO]]></singer>
<album id="22523"><![CDATA[光荣]]></album>
<remark><![CDATA[]]></remark>
<uploadinguser><![CDATA[]]></uploadinguser>
<collectinguser><![CDATA[]]></collectinguser>
<source>
<link id="3437626" filesize="5106906" format="mp3"><![CDATA[http://blog.heinekenf1.net/music/gr.mp3]]></link>
<link id="3441621" filesize="3394663" format="mp3"><![CDATA[http://space6.j.cn/olympic/edit/228/67228-3394663.mp3]]></link>
<link id="3090938" filesize="3395499" format="mp3"><![CDATA[http://space5.j.cn/olympic/convert/228/67228-3395499.mp3]]></link>
</source>
</song>
<song>...</song>
<song>...</song>
<song>...</song>
<song>...</song>
<song>...</song>
<song>...</song>
</songlist>
</info>

目前这是我的模型:

[XmlRoot("info")]
public class Response
{
[XmlElement("result")]
public Result Results { get; set; }

[XmlArray("songlist")]
[XmlArrayItem("song", typeof(Song))]
Song[] SongList { get; set; }
}
public class Result
{
[XmlAttribute("resultCode")]
public int ResultCode { get; set; }
}
public class Song
{
[XmlElement("name")]
public string Name { get; set; }

[XmlElement("singer")]
public string Artist { get; set; }

[XmlElement("album")]
public string Album { get; set; }

[XmlArray("source")]
[XmlArrayItem("link", typeof(Link))]
public Link[] Sources { get; set; }
}
public class Link
{
[XmlAttribute("filesize")]
public int FileSize { get; set; }
[XmlAttribute("format")]
public string Format { get; set; }
[XmlText]
public string URI { get; set; }
}

但是当我尝试使用下面的代码反序列化时,它没有得到正确的解析,即我没有看到 resultCode 和歌曲列表(虽然没有错误)。

XmlSerializer s = new XmlSerializer(typeof(Response), new XmlRootAttribute("info"));
Response response = (Response)s.Deserialize(data.CreateReader());

有什么提示吗?

最佳答案

首先确保所有属性都是公共(public)的,因为序列化只采用公共(public)属性。您的 Song[] 没有访问修饰符,默认为私有(private)。

以此作为反序列化您的 xml 的开始。为了让它工作,我做了一些改变。例如。使用 XmlElement 而不是 XmlArray 创建元素 SongList。

[XmlRoot("info")]
public class Response
{
[XmlElement("result")]
public Result Result { get; set; }

[XmlElement("songlist")]
public SongList SongList { get; set; }
}

public class Result
{
[XmlAttribute("resultCode")]
public int ResultCode { get; set; }

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

public class SongList
{
[XmlElement("song")]
public Song[] Songs { get; set; }
}

public class Song
{
[XmlElement("id")]
public string Id { get; set; }
}

希望对您有所帮助!

关于c# - 无法反序列化 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10032613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com