Reputation: 9737
I hate to be the village idiot, but I have no clue how Linq to XML works. I know it is supposed to be like writing a SQL query, but I just can't wrap my head around it. I am trying to get data out of an XML string. I don't want to use XMLDocuments and XPath to do this. I could do that, I did do that, I am trying to make my application a bit more snazzy. The XML that I have is a little bit onerous, but I was able to use Xpath to parse it. Linq to XML is supposed to be so much easier. From the following XML I need to get all the elements out of there and bind them to a POCO object. For instance I need to set a Patient.PatientId object with the value attribute (4563) of the element node with the PatientID name attribute. Now, I could understand how to do this using an XmlDocument. But I can't for the life of me figure out how to do this with LINQ to XML? What can I do? I have an example of what I am attempting to do below the xml that is giving me trouble.
<?xml version="1.0" encoding="utf-8"?>
<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" />
<validation>
<rules>
<rule id="r0" test="#element0.value == ''">
<fail>
<html>
<b>Patient ID is null, value must be present</b>
</html>
</fail>
</rule>
</rules>
</validation>
</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" />
<!--Patient/compositeID[./idType='populationPatientID']/id-->
<validation>
<rules>
<rule id="r1" test="#element1.value == ''">
<fail>
<html>
<b>EMPI ID is null, value must be present</b>
</html>
</fail>
</rule>
</rules>
</validation>
</element>
Again, Here is the LINQ to XML that I am trying to use.
TemplateModel template = (TemplateModel)(from templates in elem.XPathSelectElements("//templates/template")
select new PatientACOData
{
PatientId = templates.Elements("//element/element[@name='PatientId']").Attributes("value").Value;
});
UPDATE>>> THE ABOVE EXAMPLE CLASS DEFINITION WAS SLIGHTLY TO) SIMPLE... Here is the appropriate class definition...
class PatientClass
{
public int Item_ID { get; set; }
public int PatientId { get; set; }
public int EMPIID { get; set; }
//public int PopulationPatientID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
public string Phone { get; set; }
public string HostpitalFinNumber { get; set; }
public DateTime AdminDate { get; set; }
public string MRNType { get; set; }
public string MRN { get; set; }
public string PatientRoomPhone { get; set; }
public DateTime DischargeDateTime { get; set; }
public string DischargeDisposition { get; set; }
public string DischargeTo { get; set; }
public char DischargeAdvocateCall { get; set; }
public string Payor { get; set; }
public char HomeHealthCareAccepted { get; set; }
public char SafeLandingAccepted { get; set; }
public string PCPName { get; set; }
public string PCPPhone { get; set; }
public string SpecialistName { get; set; }
public string SpecialistPhone { get; set; }
public DateTime PCPAppointmentDateTime { get; set; }
public string PCPAppointmentLocation { get; set; }
public DateTime SpecialistAppointmentDateTime { get; set; }
public string SpecialistAppointmentLocation { get; set; }
public char CompletedPathway { get; set; }
public string CompletedPathwayReason { get; set; }
public string Comment { get; set; }
}
Upvotes: 0
Views: 255
Reputation: 1329
According your XML i would fill PatientClass as follows:
var xml = XElement.Load("XMLFile1.xml");
var patients = from template in xml.Element("templates").Elements("template")
select new PatientClass
{
PatientId = (from element in template.Element("elements").Elements("element")
where element.Attribute("name").Value == "PatientId"
select (int)element.Attribute("value")).FirstOrDefault()
};
Upvotes: 1