首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按更高节点过滤属性linq

按更高节点过滤属性linq
EN

Stack Overflow用户
提问于 2012-11-18 12:46:53
回答 1查看 106关注 0票数 0

我正在尝试序列化新创建的对象,并将其插入到XDocument的特定子节点中。我设法做到了这一点,但我的代码似乎很难闻。

如何在不像下面这样链接Parent属性的情况下测试更高的节点属性值?

代码语言:javascript
复制
XDocument xdoc = XDocument.Load(path);

var elements = (
    from doc in xdoc.Descendants("Unit")
    where doc.Parent.Parent.Attribute("name").Value == _UnitTypeName &&
    doc.Parent.Parent.Parent.Parent.Attribute("name").Value == _UnitCategoryN
    doc.Parent.Parent.Parent.Parent.Parent.Parent.Attribute("name").Value == 
    select doc
    );

foreach (var element in elements)
{
    Unit unit =
        new Unit
        {
            Armour = _Armour,
            Attacks = _Attacks,
            BallisticSkill = _BallisticSkill,
            Composition = _Composition,
            DedicatedTransport = _DedicatedTransport,
            Initiative = _Initiative,
            Leadership = _Leadership,
            Options = _Options,
            SaveThrow = _SaveThrow,
            SpecialRules = _SpecialRules,
            Strength = _Strength,
            Toughness = _Toughness,
            UnitName = _UnitName,
            Weapons = _Weapons,
            WeaponSkill = _WeaponSkill,
            Wounds = _Wounds,
            Points = _Points
        };

    XmlSerializer serializer = new XmlSerializer(typeof(Unit));
    using (var stream = new MemoryStream())
    {
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("", "");
        serializer.Serialize(stream, unit, ns);
        stream.Position = 0;

        //using (XmlReader reader = XmlReader.Create(stream))
        //{
        //    XElement xe = XElement.Load(reader);
        XElement xe = XElement.Load(@"C:\Test\tempfile.xml"); // For some reason loading via MemoryStream messes with xml formatting
        element.AddBeforeSelf(xe);
        //}
    }
    break;
}
xdoc.Save(path);

这是XML文档的结构:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfArmy>
  <Army name="Tyranid">
    <unit-category>
      <UnitCategory name="Troops">
        <unit-type>
          <UnitType name="Infantry">
            <unit>
              <Unit points="5" name="Hornmagant" composition="20" weapon-skill="3" ballistic-skill="100" strength="3" toughness="4" wounds="1" initiative="3" attacks="3" leadership="5" saving-throw="6+" armour="Chitin" weapons="Many" special-rules="None" dedicated-transport="No" options="8">
                <Amount>0</Amount>
              </Unit>
              <Unit points="5" name="Termagant" composition="20" weapon-skill="3" ballistic-skill="100" strength="3" toughness="4" wounds="1" initiative="3" attacks="3" leadership="5" saving-throw="6+" armour="Chitin" weapons="Many" special-rules="None" dedicated-transport="No" options="8">
                <Amount>0</Amount>
              </Unit>
            </unit>
          </UnitType>
        </unit-type>
      </UnitCategory>
    </unit-category>
  </Army>
</ArrayOfArmy>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-18 13:19:37

您可以使用,这类似于使用嵌套的foreach循环:

代码语言:javascript
复制
var elements = (
    from army in xdoc.Descendants("Army")
    where army.Attribute("name").Value == _ArmyName
    from unitCategory in army.Descendants("UnitCategory")
    where unitCategory.Attribute("name").Value == _UnitCategoryName
    from unitType in unitCategory.Descendants("UnitType")
    where unitType.Attribute("name").Value == _UnitTypeName
    from unit in unitType.Descendants("Unit")
    select unit
    );

注意:等效的方法语法是。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13437580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档