【问题标题】:How to deserialize Xml from string (Xml get from chartlyrics.com)如何从字符串反序列化 Xml(Xml 来自 chartlyrics.com)
【发布时间】:2021-11-01 19:02:17
【问题描述】:

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

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

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

System.Runtime.InteropServices.WindowsRuntime.RuntimeClass 是 由于其保护级别而无法访问。只有公共类型可以 已处理。

我的课程如下:

    [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 课程是否在任何其他课程中?
  • Selim Yildiz ,真的好点子!该课程在另一个课程(不是公共课程)内,这就是我遇到问题的原因。现在问题解决了。谢谢!
  • 我将此作为答案添加,以便对其他人有所帮助
  • 如果@SelimYildiz 解决了您的问题,请mark 接受

标签: c# uwp xmlserializer


【解决方案1】:

您的问题是SearchLyricResultCollection 可能在另一个不公开的类中。因此,如果您将其更新为公开,它应该可以解决您的问题。

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

【讨论】:

    猜你喜欢
    • 2013-01-16
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 2011-06-27
    相关资源
    最近更新 更多