Retrieve nested list from XDocument with LINQ

HyperActiv

New Member
Ok I want my link query to return a list of users. Below is the XML\[code\]<section type="Users"> <User type="WorkerProcessUser"> <default property="UserName" value="http://stackoverflow.com/questions/11057024/Main"/> <default property="Password" value=""/> <default property="Description" value=""/> <default property="Group" value=""/> </User> <User type="AnonymousUser"> <default property="UserName" value="http://stackoverflow.com/questions/11057024/Second"/> <default property="Password" value=""/> <default property="Description" value=""/> <default property="Group" value=""/> </User></section>\[/code\]And my current LINQ Query that doesn't work. doc is an XDocument\[code\]var users = (from iis in doc.Descendants("section") where iis.Attribute("type").Value =http://stackoverflow.com/questions/11057024/="Users" from user in iis.Elements("User") from prop in user.Descendants("default") select new { Type = user.Attribute("type").Value, UserName = prop.Attribute("UserName").Value });\[/code\]I get an \[code\]Object reference not set to an instance of an object\[/code\] exception. Can anyone tell me what I need to fix?Here is my second attempt after fixing for the wrong property name. However this one does not seem to enumerate the UserName value for me when I try to use it or at least when I try to write it to the console. Also this returns 8 total results I should only have 2 results as I only have 2 users.\[code\](from iis in doc.Descendants("section") where iis.Attribute("type").Value =http://stackoverflow.com/questions/11057024/="Users" from user in iis.Elements("User") from prop in user.Descendants("default") select new { Type = user.Attribute("type").Value, UserName = (from name in prop.Attributes("property") where name.Value =http://stackoverflow.com/questions/11057024/="UserName" select name.NextAttribute.Value).ToString() });\[/code\]
 
Back
Top