Linq to XML 选择子列表

发布于 2024-12-19 10:34:39 字数 1150 浏览 1 评论 0原文

XML 文件示例如下:

<?xml version="1.0" encoding="UTF-8"?>
<game>
    <name>bomber</name>
    <behaviors-used>
        <behavior id="Bullet" version="1">Bullet</behavior>
        <behavior id="Fade" version="1">Fade</behavior>
        <behavior id="Flash" version="1">Flash</behavior>
        <behavior id="Sin" version="1">Sine</behavior>
        <behavior id="scrollto" version="1">Scroll To</behavior>
    </behaviors-used>
</game>

我有这样的查询:

var data = (from item in loaded.Descendants("game")
            select new
            {
                name = item.Element("name").Value,
                behaviorlist = item.Element("behaviors-used").Value
            }).Single();

看起来工作正常。但是,我现在需要检索 behaviorlist 中的所有 元素。我似乎不能这样做:(

var bq = (from c in data.behaviorlist select new { behaviour = c.Element("behaviour")});

抛出无效的语法错误)。

如何检索所有行为,不仅访问其文本,还访问属性 idversion

An example XML file is this:

<?xml version="1.0" encoding="UTF-8"?>
<game>
    <name>bomber</name>
    <behaviors-used>
        <behavior id="Bullet" version="1">Bullet</behavior>
        <behavior id="Fade" version="1">Fade</behavior>
        <behavior id="Flash" version="1">Flash</behavior>
        <behavior id="Sin" version="1">Sine</behavior>
        <behavior id="scrollto" version="1">Scroll To</behavior>
    </behaviors-used>
</game>

I have the query:

var data = (from item in loaded.Descendants("game")
            select new
            {
                name = item.Element("name").Value,
                behaviorlist = item.Element("behaviors-used").Value
            }).Single();

Which seems to work fine. However, I need to now retrieve all the <behavior> elements in the behaviorlist. I can't seem to do it like this:

var bq = (from c in data.behaviorlist select new { behaviour = c.Element("behaviour")});

(Throws invalid syntax errors).

How do I retrive all the behaviours and not only access their text but also the properties id and version?

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

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

发布评论

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

评论(3

甜味超标? 2024-12-26 10:34:39

您的 behaviorlist 现在并不是真正的列表 - 您想要的是元素而不是组合文本,因此您应该检索 Value 属性,而不是使用父节点的 Value 属性。名称为“behavior”的 code>Elements:

var data = (from item in loaded.Descendants("game")
            select new
            {
                name = item.Element("name").Value,
                behaviorlist = item.Element("behaviors-used").Elements("behavior")
            }).Single();

从结果列表中,您可以轻松检索属性:

var bq = (from c in data.behaviorlist select new 
{ 
    id = c.Attribute("id").Value,
    version = c.Attribute("version").Value,
});

Your behaviorlist is not really a list right now - you want the elements not the combined text, so instead of using the Value property of the parent node, you should retrieve the Elements with name "behavior":

var data = (from item in loaded.Descendants("game")
            select new
            {
                name = item.Element("name").Value,
                behaviorlist = item.Element("behaviors-used").Elements("behavior")
            }).Single();

From the resulting list you can then easily retrieve the properties:

var bq = (from c in data.behaviorlist select new 
{ 
    id = c.Attribute("id").Value,
    version = c.Attribute("version").Value,
});
Oo萌小芽oO 2024-12-26 10:34:39

idversionbehavior 节点的属性:

var items =
    xDocument.Descendants("behaviors-used")
             .Descendants("behavior")
             .Select((w, i) =>
                new
                    {
                        Index = i,
                        Value = w.Value,
                        Id = w.Attribute("id").Value,
                        Version = w.Attribute("version").Value
                    })
             .ToList();

id and version are attributes of a behavior node:

var items =
    xDocument.Descendants("behaviors-used")
             .Descendants("behavior")
             .Select((w, i) =>
                new
                    {
                        Index = i,
                        Value = w.Value,
                        Id = w.Attribute("id").Value,
                        Version = w.Attribute("version").Value
                    })
             .ToList();
烟酉 2024-12-26 10:34:39

能够让它在 LinqPad 中与您的文档一起工作:

  var data = (from item in loaded.DescendantsAndSelf("game")
            select new{
                       name = item.Element("name").Value,
                       behaviorlist = item.Element("behaviours-used")
                      }
            ).Single();

  var bq = (from c in data.behaviorlist.Descendants("behaviour")
            select new {
                         behaviour = c.Value,
                         id = c.Attribute("id").Value,
                         version = c.Attribute("version").Value
                        });

并生成 5 个匿名对象的序列 {behaviour, id, version}

Was able to get this to work in LinqPad with your document:

  var data = (from item in loaded.DescendantsAndSelf("game")
            select new{
                       name = item.Element("name").Value,
                       behaviorlist = item.Element("behaviours-used")
                      }
            ).Single();

  var bq = (from c in data.behaviorlist.Descendants("behaviour")
            select new {
                         behaviour = c.Value,
                         id = c.Attribute("id").Value,
                         version = c.Attribute("version").Value
                        });

and yielded a sequence of 5 anonymous objects {behaviour, id, version}

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