0

我正在尝试将新创建的对象序列化并插入到XDocument. 我设法做到了这一点,但我的代码看起来很臭。

如何在不链接属性的情况下针对更高的节点属性值进行测试,Parent就像我在下面所做的那样?

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 文档的结构:

<?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>
4

1 回答 1

1

您可以使用复合 from 子句,这类似于使用嵌套foreach循环:

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
    );

注意:等效的方法语法是SelectMany()

于 2012-11-18T05:19:36.637 回答