【发布时间】:2012-03-09 22:16:25
【问题描述】:
我讨厌成为村里的白痴,但我不知道 Linq to XML 是如何工作的。我知道这应该就像编写 SQL 查询一样,但我就是无法理解它。我正在尝试从 XML 字符串中获取数据。我不想使用 XMLDocuments 和 XPath 来做到这一点。我可以做到,我确实做到了,我试图让我的应用程序更时髦一点。我拥有的 XML 有点繁琐,但我能够使用 Xpath 来解析它。 Linq to XML 应该更容易。我需要从下面的 XML 中取出所有元素并将它们绑定到 POCO 对象。例如,我需要使用 PatientID 名称属性的元素节点的 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;
});
更新>>> 上面的示例类定义稍微)简单...这是适当的类定义...
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 中的数据提取到数据对象中。我正在尝试几种不同的方法,但我的障碍是学习 linq to XML
-
请分享一个“数据对象”类定义
标签: linq-to-xml