如何从字符串反序列化 Xml(Xml 从 chartlyrics.com 获取)

How to deserialize Xml from string (Xml get from chartlyrics.com)

我无法反序列化从 chartlyrics.com 获得的字符串。我通过下一行代码从那里得到结果:

string xml = await HttpClient.GetStringAsync("http://api.chartlyrics.com/apiv1.asmx/SearchLyric?artist="+Artist+"&song="+Song);

到目前为止一切顺利...但是当我尝试反序列化时出现问题。在过程中我得到错误

System.Runtime.InteropServices.WindowsRuntime.RuntimeClass is inaccessible due to its protection level. Only public types can be processed.

我的 类 如下:

    [Serializable]
    public class SearchLyricResultCollection
    {
        [XmlArray("ArrayOfSearchLyricResult")]
        [XmlArrayItem("SearchLyricResult")]
        public List<SearchLyricResult> ListOfTracks;
    }
    [Serializable]
    public class SearchLyricResult
    {
        [XmlElement(ElementName = "TrackId")]
        public string TrackId;
        [XmlElement(ElementName = "LyricChecksum")]
        public string LyricChecksum;
        [XmlElement(ElementName = "LyricId")]
        public string LyricId;
        [XmlElement(ElementName = "SongUrl")]
        public string SongUrl;
        [XmlElement(ElementName = "ArtistUrl")]
        public string ArtistUrl;
        [XmlElement(ElementName = "Artist")]
        public string Artist;
        [XmlElement(ElementName = "Song")]
        public string Song;
        [XmlElement(ElementName = "SongRank")]
        public string SongRank;
    }

然后我尝试使用下一个代码进行序列化:

        //Crash just here...
        XmlSerializer serializer = new XmlSerializer(typeof(SearchLyricResultCollection));
        //Cannot continue up here...
        using (TextReader reader = new StringReader(xml))
        {
            var result = (SearchLyricResultCollection)serializer.Deserialize(reader);
        }

那么,如何解决这个问题?

你的问题是 SearchLyricResultCollection 可能在另一个 class 里面,而不是 public。因此,如果您将其更新为 public,它应该可以解决您的问题。

参见:Public Class - "is inaccessible due to its protection level. Only public types can be processed."