将 XElement 分组到 Linq to XML 中的集合中?

发布于 2024-12-01 05:22:37 字数 1266 浏览 0 评论 0原文

这是一个 XML 示例:

<ProblemFactory Name="Diagnostic exam" Count="1">
    <Condition ObjectiveID="1" Type="1" CountRanges="2" Range1Decimals="0" Range1Min="3" Range1Max="10" Range2Decimals="0" Range2Min="6" Range2Max="10" />
    <Condition ObjectiveID="1" Type="1" CountRanges="2" Range1Decimals="0" Range1Min="6" Range1Max="10" Range2Decimals="0" Range2Min="6" Range2Max="10" />
</ProblemFactory>

我正在尝试执行如下操作:

        XDocument xdoc = XDocument.Load("ProblemFactory.xml");
        var problems = from pf in xdoc.Descendants("Condition")
                       select new
                       {
                           // ObjectiveID property
                           // Type property
                           // Ranges collection property
                       };

创建范围集合是我的问题。如何构建 XML 文件或查询来创建集合?该集合将包含:RangeDecimals、RangeMin、RangeMax。

我不确定,但我可以想象要解决这个问题,我必须重新构造我的条件元组:

<Condition ObjectiveID="1" Type="1" >
    <RangesCollection>
        <Range RangeDecimals="0" RangeMin="3" RangeMax="10" />
        <Range RangeDecimals="0" RangeMin="6" RangeMax="10" />
    </RangesCollection>
</Condition>

This is an XML example:

<ProblemFactory Name="Diagnostic exam" Count="1">
    <Condition ObjectiveID="1" Type="1" CountRanges="2" Range1Decimals="0" Range1Min="3" Range1Max="10" Range2Decimals="0" Range2Min="6" Range2Max="10" />
    <Condition ObjectiveID="1" Type="1" CountRanges="2" Range1Decimals="0" Range1Min="6" Range1Max="10" Range2Decimals="0" Range2Min="6" Range2Max="10" />
</ProblemFactory>

And I'm trying to do something like the following:

        XDocument xdoc = XDocument.Load("ProblemFactory.xml");
        var problems = from pf in xdoc.Descendants("Condition")
                       select new
                       {
                           // ObjectiveID property
                           // Type property
                           // Ranges collection property
                       };

Create the ranges collection is my problem. How can I strutured my XML file or query to create the collection? The collection will contain: RangeDecimals, RangeMin, RangeMax.

I'm not sure, but I can Imagine to solve this problem I've gotta reestruct my Condition tuple:

<Condition ObjectiveID="1" Type="1" >
    <RangesCollection>
        <Range RangeDecimals="0" RangeMin="3" RangeMax="10" />
        <Range RangeDecimals="0" RangeMin="6" RangeMax="10" />
    </RangesCollection>
</Condition>

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

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

发布评论

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

评论(1

紫南 2024-12-08 05:22:37
var problems = 
    from condition in xdoc.Descendants("Condition")
    select new {
        ObjectiveID = condition.Attribute("ObjectiveID").Value,
        Type = condition.Attribute("Type").Value,
        Ranges = Enumerable
            .Range(1, (int)condition.Attribute("CountRanges"))
            .Select(i => new {
                Min = (int)condition.Attribute("Range" + i + "Min"),
                Max = (int)condition.Attribute("Range" + i + "Max"),
                Decimals = (int)condition.Attribute("Range" + i + "Decimals"),
            }).ToArray()
    };


With the new format,

Ranges = condition.Descendants("Range")
    .Select(range => new {
        Min = (int)range.Attribute("RangeMin"),
        Max = (int)range.Attribute("RangeMax"),
        Decimals = (int)range.Attribute("RangeDecimals")
    })
    .ToArray()
var problems = 
    from condition in xdoc.Descendants("Condition")
    select new {
        ObjectiveID = condition.Attribute("ObjectiveID").Value,
        Type = condition.Attribute("Type").Value,
        Ranges = Enumerable
            .Range(1, (int)condition.Attribute("CountRanges"))
            .Select(i => new {
                Min = (int)condition.Attribute("Range" + i + "Min"),
                Max = (int)condition.Attribute("Range" + i + "Max"),
                Decimals = (int)condition.Attribute("Range" + i + "Decimals"),
            }).ToArray()
    };


With the new format,

Ranges = condition.Descendants("Range")
    .Select(range => new {
        Min = (int)range.Attribute("RangeMin"),
        Max = (int)range.Attribute("RangeMax"),
        Decimals = (int)range.Attribute("RangeDecimals")
    })
    .ToArray()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文