Linq to XML,以正确的树结构获取项目
我有一些像这样的 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
对象,子选项卡对象包含 subtabdescription 属性和 subtabtext 属性。我还有一个具有展览描述和展览文件名属性的展览对象。
我需要填充这些对象,以便完成后我将拥有三个选项卡对象,每个对象包含 3 个子选项卡对象,并填充所有适当的字段。我还需要 3 个展览对象,其中包含其展览描述和展览文件名。
我之前用 ling to xml 做过一些工作,但我从来不用担心如何以 xml 的完美树表示形式获取对象,因为通常它们都有 id,我可以在事后浏览列表并对对象进行分组物品正确。对此的任何帮助都会很棒。
I have some XML like this
<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>
In my c# code I have a tab object that contains a tabdescription property and a List<subtab>
objects, and the subtab object contains a subtabdescription property and a subtabtext property. I also have an exhibit object with exhibitdescription and exhibitfilename properties.
I need to populate these objects so that when finished I would have three tab objects each containing their 3 subtab objects with all appropriate fields populated. I would also need 3 exhibit objects populated with their exhibitdescription and exhibitfilename.
I've done a little work with ling to xml before, but I never had to worry about getting the objects out in a perfect tree representation of the xml because usually they have id's where I can go through the lists after the fact and group the items properly. Any help with this would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不熟悉 LINQ to XML,您可以考虑使用 XmlSerializer 将 XML 反序列化为对象,只需添加一些属性即可。
或者使用 LINQ to XML 例如
If you are not familiar with LINQ to XML you could consider to use XmlSerializer to deserialize the XML into your objects, simply by adding some attributes.
Or use LINQ to XML e.g.