我有一些这样的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属性和List <subtab>
对象的tab对象,subtab对象包含一个subtabdescription属性和一个subtabtext属性。我还有一个展览对象,其中包含展览描述和展示文件名属性。
我需要填充这些对象,以便在完成后我将有三个选项卡对象,每个对象包含3个子选项卡对象,并填充所有适当的字段。我还需要3个展示对象,其中包含他们的展览描述和展览文件名。
之前我已经完成了对xml的一些工作,但是我从来不必担心在xml的完美树形表示中获取对象,因为通常它们具有id,我可以在事实之后通过列表并正确分组项目。任何帮助都会很棒。
答案 0 :(得分: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()
};