2011-06-14 3 views
0

에서 얻는 아이템 나는 내가 tabdescription 속성과 목록 <subtab> 객체를 포함하는 탭 개체가 내 C# 코드에서이Linq는 XML에 적절한 트리 구조

<case> 
<tab tabdescription="text here"> 
    <subtab subtab_description="subtab description here" subttab_text="subtab text here" /> 
    <subtab subtab_description="subtab description here" subttab_text="subtab text here" /> 
    <subtab subtab_description="subtab description here" subttab_text="subtab text here" /> 
</tab> 

<tab tabdescription="text here"> 
    <subtab subtab_description="subtab description here" subttab_text="subtab text here" /> 
    <subtab subtab_description="subtab description here" subttab_text="subtab text here" /> 
    <subtab subtab_description="subtab description here" subttab_text="subtab text here" /> 
</tab> 

<tab tabdescription="text here"> 
    <subtab subtab_description="subtab description here" subttab_text="subtab text here" /> 
    <subtab subtab_description="subtab description here" subttab_text="subtab text here" /> 
    <subtab subtab_description="subtab description here" subttab_text="subtab text here" /> 
</tab> 

<exhibit exhibitdescription="exhibit description here"> 
    <exhibitfilename>FileName.File</exhibitfilename /> 
</exhibit> 

<exhibit exhibitdescription="exhibit description here"> 
    <exhibitfilename>FileName.File</exhibitfilename /> 
</exhibit>` 

<exhibit exhibitdescription="exhibit description here"> 
    <exhibitfilename>FileName.File</exhibitfilename /> 
</exhibit> 
</case> 

같은 일부 XML 및 하위 탭 개체가 subtabdescription 속성과 subtabtext 속성을 포함합니다. 또한 exhibitiondescription 및 exhibitfilename 속성을 가진 표시 객체가 있습니다.

이러한 개체를 채워야 만 마칠 때마다 적절한 각 필드가 채워진 3 개의 하위 개체가 포함 된 3 개의 탭 개체가 생성됩니다. 나는 또한 그들의 전시 설명과 전시 파일명으로 채워진 3 개의 전시 객체를 필요로 할 것이다.

전에 XML에 약간의 작업을 했었지만 XML의 완벽한 트리 표현으로 객체를 가져 오는 것에 대해 걱정할 필요가 없었습니다. 일반적으로 객체의 ID를 가지고 있기 때문에 객체를 가져올 수 있습니다. 사실을 파악하고 항목을 적절하게 그룹화하십시오. 이것에 대한 도움이 될 것입니다.

답변

0

LINQ to XML에 익숙하지 않은 경우 XmlSerializer를 사용하여 XML을 개체로 역 직렬화 할 수 있습니다. 간단히 말해서 일부 특성을 추가하는 것입니다. 또는 LINQ to XML을 사용하십시오.

XDocument doc = XDocument.Load("case.xml"); 
IEnumerable<Tab> tabs = 
    from tab in doc.Root.Elements("tab") 
    select new Tab() { 
    Description = (string)tab.Attribute("tabdescription"), 
    Subtabs = (from subtab in tab.Elements("subtab") 
       select new Subtab() { 
       Subtabdescription = (string)subtab.Attribute("subtabdescription"), 
       Subtabtext = (string).subtab.Attribute("subtabtext") 
       }).ToList() 
    }; 
+0

정말 고마워요, 그 예제에서 많은 것을 배웠습니다. – Josh