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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:50:17+00:00 2026-05-29T19:50:17+00:00

I currently have an XML Structure that looks something like this <Parent> <Info> <Info-Data></Info-Data>

  • 0

I currently have an XML Structure that looks something like this

<Parent>
 <Info>
   <Info-Data></Info-Data>
   <Info-Data2></Info-Data2>
 </Info>
 <Message>
   <Foo></Foo>
   <Bar></Bar>
 </Message>
 <Message>
   <Foo/>
   <Bar/>
 </Message>
</Parent>

What I’m trying to accomplish is split each Message into it’s own unique XDocument. I want it to be

<Parent>
 <Info />
 <Message />
</Parent> 

I tried to do the following.

XDocument xDoc = XDocument.Parse(myXMLString);

IEnumerable<XElement> elements = xDoc.Descendants(xDoc.Root.Name.NameSpace + "Message");

foreach(XElement element in elements)
{
  XDocument newDoc = XDocument.Parse(element.ToString());
}

Obviously this only gets me everything from Message and below. I tried using Ancestors and AncestorsAndSelf but they always include BOTH Messages. Is there a different call I should be making?

  • 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-29T19:50:18+00:00Added an answer on May 29, 2026 at 7:50 pm

    If your format is fixed like this, it’s not so bad:

    foreach(XElement element in elements)
    {
        XDocument newDoc = new XDocument
            (new XElement(xDoc.Root.Name,
                xDoc.Root.Element("Info"),
                element));
        // ...
    }
    

    It’s not great, but it’s not horrendous. An alternative is to clone the original document, remove all the Message elements, then repeatedly clone the “gutted” version and add one element at a time to the new clone:

    XDocument gutted = new XDocument(xDoc);
    gutted.Descendants(xDoc.Root.Name.Namespace + "Message").Remove();
    
    foreach(XElement element in elements)
    {
        XDocument newDoc = new XDocument(gutted);
        newDoc.Root.Add(element);
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a XML Structure that looks like this. <sales> <item name=Games sku=MIC28306200 iCat=28
I need Xml that looks like this <foo> <bar ... /> <bar ... />
I have an XML file that looks like this: <?xml version=1.0? encoding=UTF-8 standalone=no?> <dir
I have a directory structure something like this: base subdir1 subdir2 subdir3 subsubdir1 subsubdir2
I have a XML file template like this that comes from some IC chips,
I have an xml sitemap structured like a document tree, such that it looks
I have an XML document with (basically) looks like this: ... <param> <key>age</key> <value>10</value>
Currently I have an xml structure in an app. I needed to convert it
Hi currently I have a nested XMl , having the following Structure : <?xml
I currently have a datasource from a client in the form of XML, this

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.