Parsing XML using Linq into ObservableCollection to populate xaml datagrid

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
I have an xml file like so:
<pre class="prettyprint <<span style="white-space:pre student_database>
<students type="men
<student>
<firstname>John</firstname>
<lastname>Smith</lastname>
</student>
<student>
<firstname>Bob</firstname>
<lastname>Villa</lastname>
</student>
<student>
<firstname>Wayne</firstname>
<lastname>Gretzky</lastname>
</student>
</students>
<students type="women
<student>
<firstname>Jane</firstname>
<lastname>Smith</lastname>
</student>
<student>
<firstname>Angelina</firstname>
<lastname>Jolie</lastname>
</student>
</students>
<students type="children
<student>
<firstname>Chris</firstname>
<lastname>Smith</lastname>
</student>
<student>
<firstname>Cassie</firstname>
<lastname>Mclean</lastname>
</student>
</students> </student_database> [/code]
<br/>

This xml is in a separate file.

My goal is to read the contents from the XML file and populate my xaml datagrid with it based on the selection of an xaml combobox. Here is the C# linq:
<pre class="prettyprint ObservableCollection<string> allFirstNames =
new ObservableCollection<string>(
from n in xmlDoc.Root.Elements("students")
where n.Attribute("type").Value == "men"
select n.Element("student/firstname").Value); [/code]
<br/>
Im new to c#, linq and .Net in general. My research has led me to believe I should use an ObservableCollection. The purpose of the C# code is to get a collection of firstnames for all the students of type==men. The problem I am having is that:
<pre class="prettyprint student/firstname[/code]
is incorrect syntax in the "select" part. Can anyone help me figure out how to get a collection of firstname so that my "allFirstNames" collection contains a collection of "men" "firstname"s.

Thanks!
<br/>


View the full article
 
Back
Top