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

  • Home
  • SEARCH
  • 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'm Java beginner, but I thought that when using try-catch-finally I don't have to
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
I am a beginner in C++ I am average at C. I have written
I am new to XML and Linq to XML and I just can't find
Greetings All; I am a beginner in using regex. What I want to do
iOS beginner here. I'm using the following code to save my facebook accessToken and
I'm a beginner in programming. I wrote a script to set DNS setting using
I'm using Visual Studio 2010 Express with Framework 4.0 and if I am correct,
I'm a beginner at ASP.NET and I was learning how to use it through

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.