XML:
<root>
<item>
<href>http://myurl</href>
</item>
<item>
<href>http://myurl2</href>
</item>
</root>
The XML data is stored in a database table.
Can I build a Linq query which selects the rows, extracts the XML and then, for example, extracts all the href tags? The end result would then be a list of all URLS for all selected rows.
This is my attempt, but it is not giving me what I want - which would be a list of all hrefs for all selected users. I just get a list of empty IEnumerations.
var all = from bm in MYTABLE
select new { name=bm.SPP_USER_ID, xml=(string) bm.SPP_BOOKMARKS_XML};
var docs = from x in all
select XDocument.Parse(x.xml);
var href = from h in docs
select h.Descendants("href");
Solution
There were 2 problems.
-- I guess that the query is only executed when a result is actually demanded. As I progressed from an SQL to an XML query, the resulting query became a mix of SQL and XML and thereby unexecutable. My solution was to force a result by converting the SQL query to a result list. This separated the linq-sql from linq-xml.
var docs = from x in all
select XDocument.Parse(x.xml);
var docs2 = docs.ToList(); // force result
-- The second problem was that I forgot to add the namespace to my XML query. Once I did that I got the required result
XNamespace ns = "http://acme/bookmarks";
var href = from h in docs2
select h.Descendants(ns + "href");
Thanks for all your help!
XMLdata type orvarchar?