I am thinking I am doing something wrong here. I am using XDocument.Descendants to get a grandchild element of an xml file that I am trying to parse into an object. But I am getting a null value every time I attempt to run it.
IEnumerable<PatientClass> template = (IEnumerable<PatientClass>)(from templates in xDocument.Descendants("dataTemplateSpecification")
select new PatientClass
{
PatientId = int.Parse(templates.Descendants("element").Single(el => el.Attribute("name").Value=="PatientId").ToString()),
EMPIID = int.Parse(templates.Descendants("element").Single(el => el.Attribute("name").Value=="EMPIID").ToString())
});
Let us suppose that that was the extent of the linq query that I am using…
<dataTemplateSpecification id="id1" name="name1" >
<templates xmlns="">
<template>
<elements>
<element id="element0" name="PatientId" display="Patient ID" dataType="String" visable="true" readOnly="false" value="4563">
<mapping path="//Template/TemplateData/ACOData/PATIENT_ID" />
</element>
<element id="element1" name="PopulationPatientID" display="Population Patient ID" dataType="String" visable="true" readOnly="true" enc="2098" value="6407">
<mapping path="//Template/TemplateData/ACOData/POPULATION_PATIENT_ID" />
</element>
<element id="element2" name="EMPIID" display="EMPIID" dataType="String" visable="true" readOnly="true" value="">
<mapping path="//Template/TemplateData/ACOData/EMPI" />
</element>
</elements>
</template>
</templates>
</dataTemplateSpecification>
Let us suppose that that is the xml that I am using… now, would I need namespaces to get that above query to work? I hope not. that would mean that I would have to rewrite my xml… Which would really, really be rough for me.
I’m not really sure what you’re trying to do. It looks like you’re trying to parse an XML element to an integer. The below code works for me, but I had to give the element with name
EMPIIDan integer in thevalueattribute.Please note, though, that there is probably a better way to do what you’re trying to do. I just tried to get as close to your original code as possible with my example.