c# - Linq query of XML attributes -


so i'm trying write simple query grabs of attribute xml file, nothing seems work. i've been able several other xml's reason 1 i'm working here won't cooperate. suggestions or advice hugely appreciated.

here's xml looks like.

<doc xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="name" xsi:schemalocation="[there's link here]" name="name"> <wrapper> <box_collection> <box name="test a" test="test b"/> <box name="test c" test="test d"/> <box name="test e" test="test f"/> </box_collection> </wrapper> </doc> 

here's c# code:

        xdocument customers = xdocument.load(@"c:\users\folder\file.xml");          ienumerable<string> names =             c in customers.descendants("box").attributes("name")             select c.value;          string namelist = "names:";           foreach (string c in names)         {             namer += " " + c;         }          textbox.appendtext(namelist); 

the reason xml has default namespace declared @ root element :

xmlns="name" 

xml elements inherit ancestor default namespace default, unless otherwise specified (f.e using explicit prefix point different namespace uri). can use xnamespace + element's local name point element in namespace :

xnamespace ns = "name"; ienumerable<string> names =             c in customers.descendants(ns+"box").attributes("name")             select c.value; 

Comments