xml访问包含

发布于 2025-01-05 19:23:27 字数 629 浏览 3 评论 0 原文

我有一个 xml 结构文件,其排列方式如下:

<AllReport>
    <Report>
            <DataPoint1>
            </DataPoint1>
            <DataPoint2>
            </DataPoint2> 
            <DataPoint3>
            </DataPoint3>     
    </Report>
    <Report>
            <DataPoint1>
            </DataPoint1>
            <DataPoint2>
            </DataPoint2> 
            <DataPoint3>
            </DataPoint3> 
    </Report>
     so on so forth....
</AllReport>

有没有更好的方法可以直接访问 DataPoint1 而不使用多胖 for 循环?

I have one xml structure file which is arranged like

<AllReport>
    <Report>
            <DataPoint1>
            </DataPoint1>
            <DataPoint2>
            </DataPoint2> 
            <DataPoint3>
            </DataPoint3>     
    </Report>
    <Report>
            <DataPoint1>
            </DataPoint1>
            <DataPoint2>
            </DataPoint2> 
            <DataPoint3>
            </DataPoint3> 
    </Report>
     so on so forth....
</AllReport>

Is there any better way to access DataPoint1 directly without using multi fat for loop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

破晓 2025-01-12 19:23:27

XPath 表达式 /AllReport/Report/DataPoint1//DataPoint1 都将返回示例文档中所有 DataPoint1 节点的列表。然后,您可以使用 DOM 库或所选语言提供的任何方式迭代该列表。

您使用哪个取决于实际文档的性质。如果您希望访问的每个 DataPoint1 实例都以相同的方式嵌套,则使用前者。如果您无法确定 DataPoint1 的嵌套(也许某些报表有子报表,或者 AllReport 存在一个“摘要”DataPoint1 子报表,那么

出于性能和错误检测的原因,通常最好选择更精确的表达式。

The XPath expressions /AllReport/Report/DataPoint1 and //DataPoint1 will both return a list of all the DataPoint1 nodes in your sample document. You can then iterate over that list using whatever means your DOM library or chosen language provides you.

Which you use would depend on the nature of the actual document. If every instance of DataPoint1 that you wish to access is nested in the same way, then use the former. If you cannot be sure of the nesting of DataPoint1 (perhaps some reports have subreports, or there is a "summary" DataPoint1 child of AllReport, then the latter may be preferable.

It is normally best to choose a more precise expression, both for performance and error-detection reasons.

瞎闹 2025-01-12 19:23:27

在 C# 中,您可以使用 LINQ to XML

 var xmlItems = XDocument.Load("yourXML);
 var result = from e in xmlItems.Descendants("Report").Descendants("DataPoint1");

foreach(item in result ){

  //it goes though all DataPoint 1

}

In C# you can use LINQ to XML

 var xmlItems = XDocument.Load("yourXML);
 var result = from e in xmlItems.Descendants("Report").Descendants("DataPoint1");

foreach(item in result ){

  //it goes though all DataPoint 1

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