如何用opml文件中的大纲数组反序列化大纲

本文关键字:数组 反序列化 何用 opml 文件 | 更新日期: 2023-09-27 18:04:15

我正在反序列化一个opml文件,它有一个大纲,里面有更多的大纲。如:

    <outline text="Stations"...>
           <outline.../>
           <outline.../>
            .....
    </outline>

之后有更多的奇异轮廓:

    <outline/>
    <outline/>

现在我只想反序列化"Station"提纲中的提纲。如果我直接使用Xml。反序列化,它总是包括所有的轮廓。

我有一个类大纲如下:

     public class Outline
  {
    public string Text { get; set; }
    public string URL { get; set; }
  }

我正在使用Restsharp来获得这样的响应:

        RestClient client = new RestClient("http://opml.radiotime.com/");
        RestRequest request = new RestRequest(url, Method.GET);
        IRestResponse response = client.Execute(request);
        List<Outline> outlines = x.Deserialize<List<Outline>>(response);

我成功地得到了响应,没有问题,但我只想要来自"站"大纲内的数据。

我该怎么做?如何选择"车站"轮廓?

我已经尝试使用这个类进行反序列化:

  public class Outline
  {
    public string Text { get; set; }
    public string URL { get; set; }
    public Outline[] outline {get; set;}
  }

但是这不起作用,因为只有一个Outline里面有更多的Outline。此外,我不能简单地从列表中删除轮廓,因为它们的值和名称会改变。

我想要的是,以某种方式"站"的轮廓被选中"之前"的反序列化,然后它解析其中的其余轮廓。我该如何做到这一点?

这是opml数据的url:http://opml.radiotime.com/Browse.ashx?c=local

谢谢你的帮助!

如何用opml文件中的大纲数组反序列化大纲

你基本上可以在一个长LINQ语句中解决这个问题:

class Program
{
    public static void Main()
    {
        List<Outline> results = XDocument.Load("http://opml.radiotime.com/Browse.ashx?c=local")
                                   .Descendants("outline")
                                   .Where(o => o.Attribute("text").Value == "FM")
                                   .Elements("outline")
                                   .Select(o => new Outline
                                     {
                                       Text = o.Attribute("text").Value,
                                       URL = o.Attribute("URL").Value
                                     })
                                   .ToList();
    }     
}
public class Outline
{
    public string Text { get; set; }
    public string URL { get; set; }
}

你可以改变这一行:.Where(o => o.Attribute("text").Value == "FM")像你想要的那样搜索Station,我只是使用FM,因为实际上有它的数据。