我不想成为村里的白痴,但我不知道Linq对XML的运作方式。我知道它应该像编写SQL查询一样,但我无法绕过它。我试图从XML字符串中获取数据。我不想使用XMLDocuments和XPath来执行此操作。我能做到这一点,我做到了,我试图让我的应用程序更时髦。我拥有的XML有点繁琐,但我能够使用Xpath来解析它。 Linq to XML应该更容易。从以下XML我需要获取所有元素并将它们绑定到POCO对象。例如,我需要使用具有PatientID name属性的元素节点的value属性(4563)设置Patient.PatientId对象。现在,我可以使用XmlDocument了解如何执行此操作。但我不能为我的生活弄清楚如何用LINQ to XML做到这一点?我能做什么?我有一个例子,说明我试图在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>
同样,这是我尝试使用的LINQ to XML。
TemplateModel template = (TemplateModel)(from templates in elem.XPathSelectElements("//templates/template")
select new PatientACOData
{
PatientId = templates.Elements("//element/element[@name='PatientId']").Attributes("value").Value;
});
UPDATE&GT;&GT;&GT;上面的例子类定义很简单......简单......这是适当的类定义......
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; }
}
答案 0 :(得分:1)
根据您的XML,我将填写PatientClass如下:
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()
};