.net - How to add the custom attributes value from xml element in c# using linq? -


hi have xml document

<task> <directory path="c:\backup\"/> <days value="2" /> </task> 

i want path of directory , days value in c# using linq how can achieve this?

the output should  c:\backup\ , 2 

i have far tried below xdocument path xml file works fine

              var directory = xdocument.descendants("task")                               .elements("directory")                               .attributes("path"); 

but part not working. appreciated.

you try this:

var directory = xdoc.descendantsandself("task")                    .select(c => new                    {                     path = c.elements("directory").attributes("path").first().value,                     day = c.elements("days").attributes("value").first().value,                   }); 

or if want 1 string:

var directory = xdoc.descendantsandself("task")                    .select(c => new                    {                     complete = c.elements("directory").attributes("path").first().value +                     c.elements("days").attributes("value").first().value                   }); 

edit iterate through them this:

foreach(var item in directory) {    console.writeline(item.path+ " + item.day); } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -