Q:

Get a collection of an attribute by linq

I have an xml file.
<BOOK bnumber="1" bname="Book">
    <CHP cnumber="1">
        <VER vnumber="1">This is the sentence 1.</VER>
        <VER vnumber="2">This is the sentence 2.</VER>
        <VER vnumber="3">This is the sentence 3.</VER>
   </CHP>
   <CHP cnumber="2">
        <VER vnumber="1">Hello World 1.</VER>
        <VER vnumber="2">Hello World 2.</VER>
        <VER vnumber="3">Hello World 3.</VER>
        <VER vnumber="4">Hello World 4.</VER>
   </CHP>
   <!--MANY: Thousand records-->
</BOOK>

I want to ge the attribure "cnumber". The result:
Chapter={"CHP 1";"CHP 2",....};

My uncomplected code:
XDocument xdoc = XDocument.Load("Book.xml");
        var temp = xdoc.Descendants("CHP").Where(x => x.Attribute("cnumber").Value != "0");

Thanks.

A:

It looks like you could probably use:
var chapters = xdoc.Descendants("CHP")
                   .Select(x => "CHP " + x.Attribute("cnumber").Value)
                   .ToList();

It's not clear why you need a Where clause at all - certainly none of the sample data you've given has a cnumber of 0, or an absent cnumber. If you need to take that into account, you should say so explicitly.
(Do you really need the "CHP " part to start with, btw? Why not just have a List<int>?)

