如何使用 opml 文件中的一系列轮廓反序列化轮廓

How to deserialize an outline with an array of outlines from an opml file

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

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

此后还有更多奇异的轮廓:

    <outline/>
    <outline/>

现在我只想反序列化 "Station" 大纲内的大纲。如果我直接使用 Xml.Deserializer 它总是包含所有的轮廓。

我有一个class大纲如下:

     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);

我成功收到回复,没有问题,但我只想要 "Station" 大纲中的数据。

我该怎么做?我如何 select "Stations" 大纲?

我尝试使用这个反序列化 class:

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

但这行不通,因为只有一个大纲里面有更多的大纲。我也不能简单地从列表中删除轮廓,因为值和名称会改变。

我想要的是 "Station" 大纲以某种方式 selected "before" 反序列化,然后解析其中的其余大纲。我该如何实现?

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

感谢您的帮助!

您基本上可以在一个长的 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 因为实际上有数据。