0

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 use `XDocument` to parse the opml data as it is a xml format, here is [link](http://stackoverflow.com/questions/28397741/how-to-parse-opml-with-xdocument-in-windows-phone) which may help you. – prasy Jul 23 '15 at 19:10

1 Answers1

0

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.

Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
  • Thank you, this is working quite wonderfully but I have more than a few properties in the Outline class and I can't set values of each one manually, is there someway to set the values automatically. Sorry I am a bit weak in LINQ. – Arcane Elayne Jul 23 '15 at 19:32
  • you can just add them to the `Outline` object, and add them the same way i did in the `Select`. Like `xxx = o.Attribute("xxx").Value` – Jonesopolis Jul 23 '15 at 19:36
  • Yes i can do that but it will take a lot of time to set the values one by one. Isn't there a way that sets the values automatically? Because there are a lot of properties, some twenty or so. – Arcane Elayne Jul 23 '15 at 19:44
  • you could use reflection I suppose, but I'd just hammer it out if I were you – Jonesopolis Jul 23 '15 at 21:00