2012-03-09 109 views
0

我讨厌成为村里的笨蛋,但我不知道Linq如何工作。我知道这应该就像写一个SQL查询,但我无法围绕它进行包装。我试图从XML字符串中获取数据。我不想使用XMLDocuments和XPath来执行此操作。我可以这么做,我确实这样做了,我试图让我的应用程序变得更加时髦。我拥有的XML有点麻烦,但我可以使用Xpath来解析它。 Linq到XML应该是非常容易的。从以下XML中,我需要将所有元素从那里取出并将它们绑定到POCO对象。例如,我需要使用具有PatientID名称属性的元素节点的值属性(4563)设置Patient.PatientId对象。现在,我可以了解如何使用XmlDocument来完成此操作。但是我不知道如何用LINQ to XML来做到这一点?我能做什么?我有一个例子,我正在尝试在xml中给我带来麻烦。使用LINQ to XML获取XML值使用LINQ to 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 >>>上面的例子中的类定义稍有TO)简单... ...这是适当的类定义...

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

这对你来说还是实际吗?你期待哪个输出? – sll 2012-03-12 16:53:46

+0

不确定你的意思。我正试图从该XML中将数据拉入数据对象。我尝试了几种不同的方法,但是我的障碍是学习LINQ到XML – SoftwareSavant 2012-03-12 20:35:27

+0

请分享“数据对象”类定义 – sll 2012-03-12 20:57:54

回答

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() 

       };