Query XML file with LINQ in C# -


i have linq query xml file , looks this

  ienumerable<xelement> c = cli in xel.elements(ns + "client")                                        cli.element(ns+"id").value == (((client)ccombobox.selecteditem).id +"")                                       select cli; 

it works fine.. next want iterate data

           foreach (xelement el in c)            {             } 

my xml file looks

 <client>     <id>1</id>     <name>andrej</name> 

through iteration, want extract clients values (id -> 1, name -> andrej)

my guess put el.element("name").value in middle of loop, doesn't work... oh , btw: i'm doing in c#..

what do?

btw2: can see i'm new linq think i'm way off track one...

any appriciated!! tnx!

if use code:

  public void run()   {       string filetoload = this.gettype().name + ".xml";        xelement root = xelement.load(filetoload);        var selected = cli in root.elements("client")           cli.element("id").value == "1"           select cli;        system.console.writeline("selected:");       foreach (var d in selected)           console.writeline("{0}", d.tostring());        system.console.writeline("\nitems:");       foreach (var d in selected)       {           console.writeline("id: {0}", d.element("id"));       }   } 

and source data:

<root>   <client>     <id>1</id>     <name>andrej</name>   </client>   <client>     <id>2</id>     <name>william</name>   </client>   <client>     <id>3</id>     <name>kate</name>   </client> </root> 

then... result:

selected: <client>   <id>1</id>   <name>andrej</name> </client>  items: id: <id>1</id> 

Comments