【问题标题】:LinqToXML why does my object go out of scope? Also should I be doing a group by?LinqToXML 为什么我的对象超出范围?我也应该做一个小组吗?
【发布时间】:2010-05-26 22:07:57
【问题描述】:

我有一个 IEnumerable。我需要将其转换为 XML。有一个名为“ZoneId”的属性。我需要基于这个属性编写一些 XML,然后我需要一些提供与 ZoneId 相关的数据的 decendent 元素。我知道我需要某种类型的分组。这是我迄今为止所尝试的,但没有取得多大成功。 **inventory 是一个 IEnumerable。所以我查询独特区域的库存。这工作正常。

var zones = inventory.Select(c => new {
            ZoneID = c.ZoneId
            , ZoneName = c.ZoneName
            , Direction = c.Direction
        }).Distinct();

不,我想根据区域和地点创建 xml。 ***place 是“someClass”的属性。

        var xml = new XElement("MSG_StationInventoryList"
                               , new XElement("StationInventory"
                                              , zones.Select(station =>
                                                             new XElement("station-id", station.ZoneID)
                                                             , new XElement("station-name", station.ZoneName))));

当我尝试添加“站名”元素时,这不会编译,因为“站”超出了范围。但是,我在“ZoneId”之后删除了括号,站在范围内并且我检索了站名。唯一的问题是该元素是“station-id”的后代。这不是所需的输出。他们应该是兄弟姐妹。我究竟做错了什么?最后在“站名”元素之后,我需要另一个复杂类型,即集合。称之为“地点”。它将有称为“地点”的子元素。它的数据将来自 IEnumerable,我只想要当前区域具有“ZoneId”的“地点”。谁能指出我正确的方向?从原始 IEnumerable 中选择不同的区域是错误的吗?这个对象包含了我需要的所有数据。我只需要使其具有层次结构。感谢所有指针。

干杯,
克里斯在圣地亚哥

****编辑

var zones = inventory.Select(c => new {
            ZoneID = c.ZoneId
            , ZoneName = c.ZoneName
            , Direction = c.Direction
        }).Distinct();

        var xml = new XElement("MSG_StationInventoryList"
                               , zones.Select(station => new XElement("StationInventory"
                                     , new XElement("station-id", station.ZoneID)
                                     , new XElement("station-name", station.ZoneName)
                                     , new XElement("station-travel-direction", station.Direction)
                                     , new XElement("detector-list"
                                        , inventory.Where(p=>p.ZoneId == station.ZoneID).Select(plaza=> 
                                         new XElement("detector"
                                            , new XElement("detector-id", plaza.PlazaId)))))));

这最终奏效了。

【问题讨论】:

  • 我不确定我是否了解这些地方......你能澄清一下你的意思吗?可以举个例子吗?

标签: c# linq lambda linq-to-xml


【解决方案1】:

试试这个:

zones.SelectMany(station => new object[]
{
    new XElement("station-id", station.ZoneID),
    new XElement("station-name", station.ZoneName),
    station.Places.Select(place => new XElement("place", place))
})

这会创建一个像这样的 XML 结构:

<MSG_StationInventoryList>
    <StationInventory>
        <station-id>Test1</station-id>
        <station-name>Test Station 1</station-name>
        <place>place1a</place>
        <place>place1b</place>
        <place>place1c</place>
        <station-id>Test2</station-id>
        <station-name>Test Station 2</station-name>
        <place>place2a</place>
        <place>place2b</place>
        <station-id>Test3</station-id>
        <station-name>Test Station 3</station-name>
        <place>place3a</place>
    </StationInventory>
</MSG_StationInventoryList>

这是你的意图吗?


如果您可以将 XML 结构更改为不同的结构,我建议您这样做:

<MSG_StationInventoryList>
    <StationInventory>
        <station>
            <station-id>Test1</station-id>
            <station-name>Test Station 1</station-name>
            <places>
                <place>place1a</place>
                <place>place1b</place>
                <place>place1c</place>
            </places>
        </station>
        <station>
            <station-id>Test2</station-id>
            <station-name>Test Station 2</station-name>
            <places>
                <place>place2a</place>
                <place>place2b</place>
            </places>
        </station>
        <station>
            <station-id>Test3</station-id>
            <station-name>Test Station 3</station-name>
            <places>
                <place>place3a</place>
            </places>
        </station>
    </StationInventory>
</MSG_StationInventoryList>

然后您可以简单地为每个站点和地点创建一个 XElement:

zones.Select(station => new XElement("station",
    new XElement("station-id", station.ZoneID),
    new XElement("station-name", station.ZoneName),
    new XElement("places",
        station.Places.Select(place => new XElement("place", place))))

【讨论】:

    猜你喜欢
    • 2016-05-27
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    相关资源
    最近更新 更多