使用linq to xml按元素展平XML结构 [英] Flatten XML structure by element with linq to xml

查看:68
本文介绍了使用linq to xml按元素展平XML结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近创建了一篇关于扁平化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 :-/

我有什么?

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

我需要的是以下内容:

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

因此,基本上所有元素都变为了的子节点.我确实知道,开始像这样开始解析XML文档一点也不好,但是基本上这是剩下的唯一选择,因为我们要导入数据的CMS需要这种扁平结构,而XML文档来自外部Web服务.

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.

我开始为此创建一种递归方法,但是我感到奇怪的是,可以使用一些LINQ to XML(?)使其变得更平滑(至少要尽可能平滑).最好是linq to xml,所以我希望那里有人对如何解决这个问题有所帮助? :-)

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? :-)

推荐答案

这似乎奏效-可能会有更整洁的方法,

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

这篇关于使用linq to xml按元素展平XML结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆