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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:46:11+00:00 2026-05-29T14:46:11+00:00

I recently created a post about flattening an XML structure so every element and

  • 0

I recently created a post about flattening an XML structure so every element and it’s values were turned into attributes on the root element. Got some great answer and got it working. However, sad thing is that by flattening, the client meant to flatten the elements and not make them into attributes :-/

What I have is this:

<members>
    <member xmlns="mynamespace" id="1" status="1">
       <sensitiveData>
           <notes/>
           <url>someurl</url>
           <altUrl/>
           <date1>somedate</date1>
            <date2>someotherdate</date2>
            <description>some description</description>
            <tags/>
            <category>some category</category>
        </sensitiveData>
        <contacts>
            <contact contactId="1">
                <contactPerson>some contact person</contactPerson>
                <phone/>
                <mobile>mobile number</mobile>
                <email>some@email.com</email>
            </contact>
        </kontakter>
    </member>
</members>

And what I need is the following:

<members>
    <member xmlns="mynamespace" id="1" status="1">
        <sensitiveData/>
        <notes/>
        <url>someurl</url>
        <altUrl/>
        <date1>somedate</date1>
        <date2>someotherdate</date2>
        <description>some description</description>
        <tags/>
        <category>some category</category>
        <contacts/>
        <contact contactId="1"></contact>
        <contactPerson>some contact person</contactPerson>
        <phone/>
        <mobile>mobile number</mobile>
        <email>some@email.com</email>
    </member>
</members>

So basically all elements, but flattened as childnodes of . I do know that it’s not pretty at all to begin parsing XML documents like this, but it’s basically the only option left as the CMS we’re importing data to requires this flat structure and the XML document comes from an external webservice.

I started to make a recursive method for this, but I’ve got an odd feeling that it could be made smoother (well, as smooth as possible at least) with some LINQ to XML (?) I’m not the best at linq to xml, so I hope there’s someone out there who would be helpful to give a hint on how to solve this? 🙂

  • 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-29T14:46:13+00:00Added an answer on May 29, 2026 at 2:46 pm

    This seems to work – there may be neater approaches, admittedly:

    var doc = XDocument.Load("test.xml");
    XNamespace ns = "mynamespace";
    var member = doc.Root.Element(ns + "member");
    
    // This will *sort* of flatten, but create copies...
    var descendants = member.Descendants().ToList();
    
    // So we need to strip child elements from everywhere...
    // (but only elements, not text nodes). The ToList() call
    // materializes the query, so we're not removing while we're iterating.
    foreach (var nested in descendants.Elements().ToList())
    {
        nested.Remove();
    }
    member.ReplaceNodes(descendants);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently created a custom ping box (chat). I have a auto refresh every
Recently I posted a question about the html helper dropdownlist and got it working
I've recently created several custom post types in a wordpress site, with the code
I recently created a advanced form with elements that use jquery's $().hide & $().show
I recently created my simple log4net database table for logging via the adonet appender..
I recently created a class which has a constructor taking 3 enumerations as arguments.
I recently created a generic Matrix<T> class that acts as a wrapper around a
I recently created a function in javascript that takes in a file name and
I recently created my first bash script, and I am having problems perfecting it's
I recently created a ajax based instant messaging application and after running for a

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.