开发者

Parsing XML inside a linq query

I have a list of pages collected from my database. Each page has an XML field, which looks something like this:

<items>
  <item id="153"/>
  <item id="147"/>
</items>

Now I only want the pages which points to a specific id within the XML. So something like this:

var pages = GetAll().Where(p => p.X开发者_开发技巧mlField //this is where i'm lost

I'd like to do something like this:

p.XmlField.Descendants().Where(x => x.Attribute("id") == id


If you want to check whether any item within the XmlField has the right ID:

var pages = GetAll().Where(p => p.XmlField
                                 .Descendants("item")
                                 .Any(x => (int) x.Attribute("id") == id));

Alternatively you might want to use something like:

var pages = from page in GetAll()
            from item in page.XmlField.Descendants("item")
            where (int) item.Attribute("id") == id
            select page;

That will give you repeats of pages if the XML has the same ID twice though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜