Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8878299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:40:48+00:00 2026-06-14T19:40:48+00:00

I am trying to serialize and insert a new created object into a specific

  • 0

I am trying to serialize and insert a new created object into a specific child node of an XDocument. I’ve managed to accomplish this but my code seems smelly.

How can I test against a higher nodes attribute value without chaining through the Parent property like I have done below?

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

This is the structure of the XML Document:

<?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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:40:50+00:00Added an answer on June 14, 2026 at 7:40 pm

    You can use compound from clauses, which is similar to using nested foreach loops:

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

    Note: the method syntax equivalent is SelectMany().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to insert serialize values into ac_services table but getting error: syntax
I have been trying to serialize a pretty big object. This object uses dictionaries
I'm trying to serialize into a Xml File an object which is declared as
I'm trying to insert into XML column (SQL SERVER 2008 R2), but the server's
I'm trying to insert some dynamically generated html (via PHP) into a table, but
I am trying to insert a serialize data into mySQL using PDO and I'm
I am trying to insert a whole Java object into a MongoDB Collection using
I am trying to serialize a PagedList object ( https://github.com/martijnboland/MvcPaging/blob/master/src/MvcPaging/PagedList.cs ) to Json, like
I'm trying to serialize an object to XML that has a number of properties,
I've been trying to serialize an XML (jQuery object) to string to POST it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.