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 3698844
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:01:52+00:00 2026-05-19T05:01:52+00:00

I am a beginner in using LINQ. I have an xml data with the

  • 0

I am a beginner in using LINQ. I have an xml data with the format below

<Root>
    <Global>
    </Global>
    <local>
        <element name="A">
            <subelement name="A">
                <element name="A1">
                    <subelement name="A1">
                        <Property>
                        </Property>
                    </subelement>
                </element>
                <element name="B">
                    <Property>
                    </Property>
                </element>
            </subelement>
            <subelement name="B">
                <element name="A">
                    <Property>
                    </Property>
                </element>
                <element name="B">
                    <Property>
                    </Property>
                </element>
            </subelement>
        </element>
    </local>
</Root>

I want to save each section of the Property element into it’s own xml file as below while keeping the hierarchy structure intact

<Root>
    <Global>
    </Global>
    <local>
        <element name="A">
            <subelement name="A">
                <element name="A1">
                    <subelement name="A1">
                        <Property>
                        </Property>
                </subelement>
                </element>
            </subelement>
        </element>
    </local>
</Root>

and

<Root>
    <Global>
    </Global>
    <local>
        <element name="A">
            <subelement name="A">
                <element name="B">
                    <Property>
                    </Property>
                </element>
            </subelement>
        </element>
    </local>
</Root>

and so on for all the Property elements. Each element can have a child subelement and each subelement can have element child

Can you provide a solution on how to save each section of Property element as a new xml file with the current hierarchy structure intact. I have tried using the Ancestor() method of the XElement class

XDocument xml = XDocument.Load(filename);
XDocument convertedQuery = new XDocument(
        new XElement(xml.Root.Name, from x in xml.Descendants("local")
                                    select x.Ancestors())
         );

but it is returning other Property elements as well, as below

<Root>
<Global></Global>
<local>
  <element name="A">
    <subelement name="A">
      <element name="A1">
        <subelement name="A1">
          <Property></Property>
        </subelement>
      </element>
      <element name="B">
        <Property></Property>
      </element>
    </subelement>
    <subelement name="B">
      <element name="A">
        <Property></Property>
      </element>
      <element name="B">
        <Property></Property>
      </element>
    </subelement>
  </element>
</local>

Any help would be much appreciated. Thanks

  • 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-05-19T05:01:52+00:00Added an answer on May 19, 2026 at 5:01 am

    Here is some C# sample that should do what you want or at least give you an idea on how to approach that. The sample for testing simply writes each created XDocument to Console.Out but of course you could change that to save to a file on disk:

    static void Main(string[] args)
    {
        XDocument doc = XDocument.Load(@"..\..\XMLFile1.xml");
        foreach (XElement prop in doc.Descendants("Property"))
        {
            XDocument result = new XDocument(
                new XElement(doc.Root.Name, doc.Root.Attributes(),
                    doc.Root.Elements("Global"),
                    CopySubtree(prop.Ancestors("local").First(), prop)));
            result.Save(Console.Out);
            Console.WriteLine();
        }
    }
    static XElement CopySubtree(XElement ancestor, XElement descendant)
    {
        if (ancestor == descendant)
        {
            return descendant;
        }
        else
        {
            return
                new XElement(
                    ancestor.Name, 
                    ancestor.Attributes(), 
                    CopySubtree(
                      ancestor
                      .Elements()
                      .First(e => e.DescendantsAndSelf().Any(d => d == descendant)),
                      descendant));
        }
    }
    

    [edit]
    For the new requirement to copy not only the Property ancestors but also its siblings you could adapt above method as follows:

    static XElement CopySubtree(XElement ancestor, XElement descendant)
    {
        if (ancestor == descendant.Parent)
        {
            return ancestor;
        }
        else
        {
            return
                new XElement(
                    ancestor.Name, 
                    ancestor.Attributes(), 
                    CopySubtree(
                      ancestor
                      .Elements()
                      .First(e => e.DescendantsAndSelf().Any(d => d == descendant)),
                      descendant));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using
I am a beginner in LINQ. I am using .NET 3.5. and VS 2008.
I'm Java beginner, but I thought that when using try-catch-finally I don't have to
The very common beginner mistake is when you try to use a class property
Beginner here: In this answer to my question of how to insert data into
I'm just playing with some linq and asp.net - absolute beginner so I'm not
OK, I am amittedly new to LINQ and have spent the last week reading
I've been programming for a while and have used LINQ-To-SQL and LINQ-To-Entities before (although
I'm an absolute beginner using Joomla. I'm trying to load a module within another
Folks, Debugging a .net 4.0 app using WinDbg (I'm a beginner to WinDbg). I'm

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.