in an console project I create an XDocument and using linq to get xml elements. See following code. I try to use the same in a portable class library but it does not work. Is there a different between console projects and portable class library by using linq to xml?
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(url);
Stream strm = await response.Content.ReadAsStreamAsync();
XDocument doc = XDocument.Load(strm);
var de = from el in doc.Descendants("outline")
select new
{
title = (string)el.Element("title")
};
In the following the xml example:
<opml version="2.0">
<head>
<title>Outline of Category 41</title>
<dateCreated>Tue, 26 Nov 2013 10:15:02 +0100</dateCreated>
<docs>http://www.test.com</docs>
</head>
<body>
<outline title="Title1"/>
<outline title="Title2" />
</body>
</opml>
You're looking for an element called title
- but it's actually an attribute. The same code would fail on desktop too. You want:
title = (string)el.Attribute("title")
It's not really clear why you need an anonymous type here - you could just use:
var titles = doc.Descendants("outline")
.Select(x => (string) x.Attribute("title"));
(You can do this with a query expression, but personally I wouldn't - query expressions are great for complex queries, but when you're just filtering or projecting they add more fluff than they save.)
See more on this question at Stackoverflow