bomortensen
bomortensen

Reputation: 3396

Flatten XML structure by element with linq to xml

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>[email protected]</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>[email protected]</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? :-)

Upvotes: 0

Views: 2854

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500825

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

Upvotes: 2

Related Questions