나는 여기서 뭔가 잘못하고 있다고 생각합니다. XDocument.Descendants를 사용하여 객체로 파싱하려고하는 XML 파일의 손자 엘리먼트를 가져 왔습니다. 그러나 그것을 실행할 때마다 널 값을 얻고 있습니다. null 값을 반환하는 XDocument.Descendants
IEnumerable<PatientClass> template = (IEnumerable<PatientClass>)(from templates in xDocument.Descendants("dataTemplateSpecification")
select new PatientClass
{
PatientId = int.Parse(templates.Descendants("element").Single(el => el.Attribute("name").Value=="PatientId").ToString()),
EMPIID = int.Parse(templates.Descendants("element").Single(el => el.Attribute("name").Value=="EMPIID").ToString())
});
우리가 내가 사용하고있는 LINQ 쿼리의 정도라고 가정하자
...<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" />
</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" />
</element>
<element id="element2" name="EMPIID" display="EMPIID" dataType="String" visable="true" readOnly="true" value="">
<mapping path="//Template/TemplateData/ACOData/EMPI" />
</element>
</elements>
</template>
</templates>
</dataTemplateSpecification>
우리가 내가 지금 ... 사용하고있는 XML이라고 가정하자, 위 쿼리를 작동 시키려면 네임 스페이스가 필요합니까? 내가하지 희망. 그게 내 XML을 재작 성해야 할 것입니다 ... 정말, 정말 나를 위해 거친 것입니다.
당신은'xDocument.Descendants() 경우 (X => x.Name.LocalName == "dataTemplateSpecification")을 시도 할 수 있습니다. – Bazzz
게시하기 전에 코드를 정리하십시오. 주석 처리 된 코드가 있고 줄 바꿈을 삽입하기 전에 코드가 얼마나 넓어야합니까? –
당신의 코드는 전체 요소를'int'로 파싱하려하기 때문에'int.Parse()'에'FormatException'을 던져서 작동하지 않을 것입니다. 그러나 그것은 나를 위해'null '을 반환하지 않습니다. 'dataTemplateSpecification'은 실제로 문서의 루트입니까? 그렇지 않다면 실제 루트는'xmlns' 속성을 가지고 있습니까? – svick