我一直在尝试从XML字符串填充对象。我是Linq的一个完整的nincompoop,所以我决定将Linq和XPath结合起来......这是悲伤的尝试......
public static PatientClass getDataFromXMLintoPOCO(string xml)
{
PatientClass patClass = null;
string xmlFromSvc = xml;
XDocument xDocument = XDocument.Parse(xmlFromSvc);
XElement elem = xDocument.Element("dataTemplateSpecification");
PatientClass template = (PatientClass)(from templates in elem.XPathSelectElements(string.Format("//templates/template[./elements/element[@name=\"PopulationPatientID\"and @value='{0}' and @enc='{1}']]", "1", 0))
select new PatientClass
{
PatientId = int.Parse(templates.XPathSelectElement("elements/element[@name='PatientId']").Attribute("value").Value),
EMPIID = int.Parse(templates.XPathSelectElement("elements/element[@name='EMPIID']").Attribute("value").Value),
//public int PopulationPatientID { get; set; }
FirstName = templates.XPathSelectElement("elements/element[@name='FirstName']").Attribute("value").Value,
LastName = templates.XPathSelectElement("elements/element[@name='LastName']").Attribute("value").Value,
DateOfBirth = DateTime.Parse(templates.XPathSelectElement("elements/element[@name='DateOfBirth']").Attribute("value").Value),
Phone = templates.XPathSelectElement("elements/element[@name='Phone']").Attribute("value").Value,
HostpitalFinNumber = templates.XPathSelectElement("elements/element[@name='HostpitalFinNumber']").Attribute("value").Value,
AdminDate = DateTime.Parse(templates.XPathSelectElement("elements/element[@name='AdminDate']").Attribute("value").Value),
MRNType = templates.XPathSelectElement("elements/element[@name='MRNType']").Attribute("value").Value,
MRN = templates.XPathSelectElement("elements/element[@name='MRN']").Attribute("value").Value,
PatientRoomPhone = templates.XPathSelectElement("elements/element[@name='PatientRoomPhone']").Attribute("value").Value,
DischargeDateTime = DateTime.Parse(templates.XPathSelectElement("elements/element[@name='DischargeDateTime']").Attribute("value").Value),
DischargeDisposition = templates.XPathSelectElement("elements/element[@name='DischargeDisposition']").Attribute("value").Value,
DischargeTo = templates.XPathSelectElement("elements/element[@name='DischargeTo']").Attribute("value").Value,
DischargeAdvocateCall = templates.XPathSelectElement("elements/element[@name='DischargeAdvocateCall']").Attribute("value").Value.ToCharArray()[0],
Payor = templates.XPathSelectElement("elements/element[@name='Payor']").Attribute("value").Value,
HomeHealthCareAccepted = templates.XPathSelectElement("elements/element[@name='HomeHealthCareAccepted']").Attribute("value").Value.ToCharArray()[0],
SafeLandingAccepted = templates.XPathSelectElement("elements/element[@name='SafeLandingAccepted']").Attribute("value").Value.ToCharArray()[0],
PCPName = templates.XPathSelectElement("elements/element[@name='PCPName']").Attribute("value").Value,
PCPPhone = templates.XPathSelectElement("elements/element[@name='PCPPhone']").Attribute("value").Value,
SpecialistName = templates.XPathSelectElement("elements/element[@name='SpecialistName']").Attribute("value").Value,
SpecialistPhone = templates.XPathSelectElement("elements/element[@name='SpecialistPhone']").Attribute("value").Value,
PCPAppointmentDateTime = DateTime.Parse(templates.XPathSelectElement("elements/element[@name='PCPAppointmentDateTime']").Attribute("value").Value),
PCPAppointmentLocation = templates.XPathSelectElement("elements/element[@name='PCPAppointmentLocation']").Attribute("value").Value,
SpecialistAppointmentDateTime = DateTime.Parse(templates.XPathSelectElement("elements/element[@name='SpecialistAppointmentDateTime']").Attribute("value").Value),
SpecialistAppointmentLocation = templates.XPathSelectElement("elements/element[@name='SpecialistAppointmentLocation']").Attribute("value").Value,
CompletedPathway = templates.XPathSelectElement("elements/element[@name='CompletedPathway']").Attribute("value").Value.ToCharArray()[0],
CompletedPathwayReason = templates.XPathSelectElement("elements/element[@name='CompletedPathwayReason']").Attribute("value").Value,
Comment = templates.XPathSelectElement("elements/element[@name='Comment']").Attribute("value").Value
}).AsEnumerable();
//return template != null ? template : null;
return patClass != null ? patClass : null;
}
}
我想填充的对象是这样的......
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; }
}
XML非常庞大而且非常繁琐。这让普通人感到恐惧,我可以向你保证。我删掉了大部分内容,因此它看起来很可读...
<?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>
<element id="element2" name="EMPIID" display="EMPIID" dataType="String" visable="true" readOnly="true" value="">
<mapping path="//Template/TemplateData/ACOData/EMPI" />
<!--//Templates/Patient/sources/source/empi"/>-->
<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>
<element id="element2" name="PRELOADMPACMRN" display="MPACMRN" dataType="String" visable="true" readOnly="true" value="">
<mapping path="//Template/Patient/sources/source/mpacmrn" />
<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>
</elements>
<dataTypeSpecifications>
<dataTypeSpecification id="" baseType="KeyValuePair">
<dictionaryDefinition>
<item key="0" value="-SELECT-" />
<item key="1" value="YES" />
<item key="2" value="NO" />
</dictionaryDefinition>
</dataTypeSpecification>
</dataTypeSpecifications>
</template>
</templates>
</dataTemplateSpecification>
显然,我遇到了各种各样的投射错误。第一个问题,要做到这一点比我现在正在做的更简单。任何清理这个的建议。另外,为什么我会收到投射错误?任何帮助将不胜感激。
答案 0 :(得分:1)
考虑到输入XML文档的复杂性,我希望您需要一个非常复杂的C#代码来解析它并转换为您给出的类!
我要指出的一件事是你的XPath如“elements / element [@ name ='PatientId']”可以转换为等效的Linq查询,如下所示:
templates.Descendents("element").Single(el => el.Attribute("name")=="PatientId")
这并不能让你的代码变得更简单,但它至少意味着你没有混合使用Linq和XPath。
答案 1 :(得分:1)
您粘贴的XML格式不正确,但在纠正之后,这有效:
string expression = string.Format("//templates/template[./elements/element[@name=\"PopulationPatientID\"and @value='{0}' and @enc='{1}']]", "6407", "2098");
IEnumerable<XElement> elements = elem.XPathSelectElements(expression);
IEnumerable<PatientClass> matchingPatients =
from templates in elements select new PatientClass
{
PatientId =
int.Parse(
templates.XPathSelectElement("elements/element[@name='PatientId']").Attribute(
"value").Value),
[... whatever you had before.. ]