tags:

views:

43

answers:

1

hello

i'm trying to load some elements from a xml file. but it XDocument.Load seems not treating xml file properly in this case, the method returns the content of the xml file as one node.

here is my xml content:

<processes>
 <process>winamp</process>
 <process>Acrobat</process>
 <process>WinRAR</process>
</processes>

and the code that reads the file:

 XDocument loaded = XDocument.Load("/process_list.xml");
   var x = from a in loaded.Descendants("processes")
            select a.Element("process");
    foreach (var t in x)
    {
            Console.WritleLine(t.Value.ToString());
    }

thank you

+3  A: 

Your code selects the first process element from each processes element in the document -- of which there is only one.

To select all process elements in the document, try this:

XDocument doc = XDocument.Load("process_list.xml");

foreach (var element in doc.Descendants("process"))
{
    Console.WritleLine(element.Value);
}
dtb
so how can i make it select all the "process" elements.
aleo
try using the code dtb provided...
Foole
thanks it's working.
aleo