c# - Linq to XML: create an anonymous object with element attributes and values -


I'm new to linux and I try to ask an XML document to find a list of account managers for a particular document I am doing the user (I know that it can understand more about putting it into a database or something else, but this scenario calls for an XML document).

  & lt; User emailAddress='user@fabrikam.com '& gt; & Lt; AccountManager section = 'Customer service' title = 'manager' & gt; Manager@fabrikam.com< / AccountManager & gt; & Lt; AccountManager department = 'sales' title = 'account manager' & gt; Manager@fabrikam.com< / AccountManager & gt; & Lt; AccountManager department = 'sales' title = 'account manager' & gt; Manager@fabrikam.com< / AccountManager & gt; & Lt; / User & gt; I am trying to create a list of Objects (Anonymous Type?) With the properties of both Attribute Attributes (Emails) and Value (Email). I know that I can get either of them, but my problem is to choose both.  

Here's what I'm trying to do:

  var manager = _xDoc.Root.Descendants ("user"). Where (D => D. attribute ("email address"). Value == "user@fabrikam.com"). Select (u = & gt; u.Descendants ("Account Manager"). Select (a => a.value)); The foreach (diversified managers in managers) {// do stuff}  

I can get at a.value and a.Attribute But I can not understand how to store both and in one object, I think that something like this might be showing:

  choose new {department = u .Attribute ("department"). Value, title = u.Attribute ("title"). Value, email = u.Value};  

This will look exactly the way you are right.

For example:

  _xDoc.Root.Descendants ("user"). Where (D => D. attribute ("email address"). Value == "user@fabrikam.com") .SelectMany (u = & gt; u.Descendants ("Account Manager")). Select (a => New {Department = A. Attribute ("Department"). Value, Title = a.Attribute ("Title"). Value, Email = a.Value});  

Edit : Using syntax to understand the query:

  _xDoc.Root.Descendants ("user" ) Where in you Attribution ("Email Address"). From Price == "user@fabrikam.com", select new {department = a.Attribute ("department") from U.Descendents ("Account manager"). Value, title = a.Attribute ("title"). Value, email = a.value});  

Comments