2011-06-14 23 views
0

越来越项目,我有一些这样的XML的LINQ to 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> 

在我的C#代码,我有一个包含tabdescription属性和列表<subtab>对象标签对象,并且子选项卡对象包含一个subtabdescription属性和一个subtabtext属性。我还有一个展品对象,具有Exhibdescription和exhibitfilename属性。

我需要填充这些对象,以便完成后我将有三个选项卡对象,每个对象都包含其3个子选项卡对象,并填充所有适当的字段。我还需要3个展览对象,它们的展品描述和展品文件名。

我之前对ling的工作做过一些小小的工作,但是我从来不用担心把对象放在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 2011-06-14 14:18:24