Linq to XML,以正确的树结构获取项目

发布于 2024-11-15 12:12:32 字数 1932 浏览 2 评论 0原文

我有一些像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡月浅 2024-11-22 12:12:33

如果您不熟悉 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()
  };

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.

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()
  };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文