c# - Get a collection of an attribute by linq -


i have xml file.

<book bnumber="1" bname="book"> <chp cnumber="1"> <ver vnumber="1">this sentence 1.</ver> <ver vnumber="2">this sentence 2.</ver> <ver vnumber="3">this 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 ge attribure "cnumber". 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.

it looks use:

var chapters = xdoc.descendants("chp") .select(x => "chp " + x.attribute("cnumber").value) .tolist(); 

it's not clear why need where clause @ - none of sample data you've given has cnumber of 0, or absent cnumber. if need take account, should explicitly.

(do need "chp " part start with, btw? why not have list<int>?)


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -