我正在尝试反序列化Spotify Web服务调用,并且我收到以下错误:There is an error in XML document (1, 2).
我在想它可能是因为我使用了错误的XML命名空间,但我不能找一个有效的。我无法弄清楚我做错了什么。
这是我正在使用的代码
XmlSerializer serializer = new XmlSerializer(typeof(Album), "http://www.spotify.com/ns/music/1");
Album a = new Album();
using (StreamReader reader = new StreamReader("../../AlbumSearch.xml"))
{
try
{
a = (Album)serializer.Deserialize(reader);
}
catch (Exception e)
{
Console.WriteLine("Message: {0}\n\nInner Exception: {1}", e.Message, e.InnerException);
}
}
输出
Message: There is an error in XML document (1, 2).
Inner Exception: System.InvalidOperationException: <albums xmlns='http://www.spo
tify.com/ns/music/1'> was not expected.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderAlbum.
Read7_Album()
XML示例
<?xml version="1.0" encoding="utf-8"?>
<albums xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.spotify.com/ns/music/1">
<opensearch:Query role="request" startPage="1" searchTerms="foo"/>
<opensearch:totalResults>255</opensearch:totalResults>
<opensearch:startIndex>0</opensearch:startIndex>
<opensearch:itemsPerPage>100</opensearch:itemsPerPage>
<album href="spotify:album:1zCNrbPpz5OLSr6mSpPdKm">
<name>Greatest Hits</name>
<artist href="spotify:artist:7jy3rLJdDQY21OgRLCZ9sD">
<name>Foo Fighters</name>
</artist>
<id type="upc">884977373295</id>
<popularity>0.84</popularity>
<availability>
<territories>
AD AR AT AU BE BG BO BR CA CH CL CO CR CY CZ DE DK DO EC EE ES FI FR GB GR GT HK HN HU IE IS IT LI LT LU LV MC MT MX MY NI NL NO NZ PA PE PH PL PT PY RO SE SG SI SK SV TR TW US UY
</territories>
</availability>
</album>
</albums>
答案 0 :(得分:0)
您的相册类的内容是什么样的?
那里有[XmlRoot("albums", Namespace = "http://www.spotify.com/ns/music/1")]
吗?