我是反序列化一个opml文件,其中有一个大纲,其中有更多的轮廓。比如:
<outline text="Stations"...>
<outline.../>
<outline.../>
.....
</outline>在此之后,还有更多的单数轮廓:
<outline/>
<outline/>现在,我只想反序列化“车站”大纲内的轮廓。如果我使用直接Xml.Deserializer,它总是包括所有的轮廓。
我有一个课程大纲如下:
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;}
}但这是行不通的,因为只有一个大纲中有更多的轮廓。另外,我不能简单地从列表中删除大纲,因为有值和名称会改变。
我想要的是在“反序列化”之前选择“站点”大纲,然后它解析其内部的其余轮廓。我怎样才能做到这一点?
这是opml数据的url:http://opml.radiotime.com/Browse.ashx?c=local
谢谢你的帮助!
发布于 2015-07-23 19:16:53
从本质上说,您可以在一条长长的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,因为它实际上有数据。
https://stackoverflow.com/questions/31595839
复制相似问题