简体   繁体   中英

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

I am deserializing an opml file which has one outline with more outlines inside it. Like:

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

After this there are more singular outlines:

    <outline/>
    <outline/>

Now I want to deserialize only the outlines inside the "Station" outline. If I use direct Xml.Deserializer it always includes all the outlines.

I have a class Outline as follows:

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

I am using Restsharp to get a response like this:

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

I get the response successfully, no problems there but I want only data from inside the "Station" outline.

How do I do this? How do I select the "Stations" outline?

I have tried to deserialize using this class:

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

but this doesn't work because only one Outline has more outlines inside it. Also I can't simply remove outlines from the List because there values and names alter.

What I want is that somehow the "Station" outline is selected "before" the deserializing and then it parses the rest of the outlines inside it. How do I achieve this?

This is the url for the opml data: http://opml.radiotime.com/Browse.ashx?c=local

Thank you for your help!

you can essentially knock this out in one long LINQ statement:

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

you can alter this line: .Where(o => o.Attribute("text").Value == "FM") to search for Station like you wanted, I just used FM because there was actually data for it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM