I am trying to use LINQ with lambda syntax to load a List of objects from an XML fragment but I am having trouble with the lambda syntax in creating the new object.
Here is the custom class that will hold the data:
public class PhysicianInfoModel
{
string FirstName;
string LastName;
string Degree;
}
Here is the sample XML I am trying to load the object collection from:
<response>
<control>
<status>success</status>
<dtdversion>3.0</dtdversion>
</control>
<operation>
<authentication>
<status>success</status>
<userid>jsmith</userid>
</authentication>
<result>
<status>success</status>
<physicianlist>
<physician>
<lastname degree="MD">Smith</lastname>
<firstname>Paul</firstname>
</physician>
<physician>
<lastname degree="DO">Smith</lastname>
<firstname>Paul</firstname>
</physician>
</physicianlist>
</result>
</operation>
</response>
I have tried using code such as the following but I know it is not correct as even intellisense in Visual Studio is going all "red squiggly line". In this example xml
is an XDocument
object that has loaded the XML listed above.
List<PhysicianInfoModel> nodeList = xml.Descendants("physicianlist")
.Descendants("physician")
.Select(x => new PhysicianInfoModel()
{
FirstName = x.?????,
LastName = x.????,
Degree = x.Attribute("degree") // Not working
});