Select statement for xdoc query

zeroflow

New Member
I am trying to add a sub category to Messages in my xml statement Is there a way I can do this \[code\]GroupMessages -> Message -> GroupMessage\[/code\] :\[code\] var groups = xDoc.Descendants("Group") .Select(n => new { GroupName = n.Element("GroupName").Value, GroupHeader = n.Element("GroupHeader").Value, TimeCreated = DateTime.Parse(n.Element("TimeAdded").Value), Tags = n.Element("Tags").Value, Messages = n.Element("GroupMessages").Value //line above }) .ToList(); dataGrid2.ItemsSource = groups;\[/code\]In my method GroupMessages contains both MessageID and GroupMessage and it is listing both in my datagrid within the one container. So I tried this but it lists nothing:\[code\] Messages = n.Descendants("GroupMessages").Select(nd => nd.Element("GroupMessage").Value)\[/code\]My XML looks like this:\[code\]<Group><TimeAdded>2012-04-27T10:23:50.7153613+01:00</TimeAdded><GroupName>Group</GroupName><GroupHeader>Header</GroupHeader><GroupMessages><Message><MessageID>1</MessageID><GroupMessage>Message</GroupMessage><MessageGroup/></Message></GroupMessages></Group>\[/code\]I have also tried:\[code\]Messages = n.Descendants("GroupMessages").Select(nd => nd.Descendants("Message").Select(nde => nde.Element("GroupMessage").Value))\[/code\]To no avail?Update:\[code\] private void ListGroups_Click(object sender, RoutedEventArgs e) { string uriGroup = "http://localhost:8000/Service/Group"; XDocument xDoc = XDocument.Load(uriGroup); var groups = xDoc.Descendants("Group") .Select(n => new { GroupName = n.Element("GroupName").Value, GroupHeader = n.Element("GroupHeader").Value, TimeCreated = n.Element("TimeAdded").Value, Tags = n.Element("Tags").Value, Messages = n.Element("GroupMessages").Descendants("Message").Select(nd => new { //Id = nd.Element("MessageID").Value, Message = nd.Element("GroupMessage").Value }).FirstOrDefault() }) .ToList(); dataGrid2.ItemsSource = groups; }\[/code\]Unfortunatley this method shows "Collection" inside the cell in the datagrid. If I try ToArray it will show an array message inside the cell. Is there a way to actually display the GroupMessage? Not sure how you set the child elements of a datagrid?
 
Back
Top