使用LINQ重组XML

时间:2012-04-21 00:38:43

标签: visual-studio-2010 linq c#-4.0

我有一个XML文件

<products>
<product number="241">
    <investment number="29">
        <rateLevel number="2">
            <ticker>ttt</ticker>
            <Cusip>1234</Cusip>
            <secId>ffff</secId>
            <investmentName>google</investmentName>
            <marketingStatusCode/>
            <marketingStatusReason/>
            <tiers>
                <tier>R6</tier>
            </tiers>
        </rateLevel>
    </investment>
</product>
<product number="241">
    <investment number="28">
        <rateLevel number="3">
            <ticker>bbb</ticker>
            <Cusip>233</Cusip>
            <secId>tiee</secId>
            <investmentName>apple</investmentName>
            <marketingStatusCode/>
            <marketingStatusReason/>
            <tiers>
                <tier>R2</tier>
            </tiers>
        </rateLevel>
    </investment>
</product>
<product number="200">
    <investment number="8214">
        <rateLevel number="20">
            <ticker>yyyy</ticker>
            <Cusip>5655</Cusip>
            <secId>ghos</secId>
            <investmentName>microsof</investmentName>
            <marketingStatusCode/>
            <marketingStatusReason/>
            <tiers>
                <tier>Signature</tier>
            </tiers>
        </rateLevel>
    </investment>
</product>

并希望重新格式化/重新组织(产品)标签(使用Linq)

<products>
<product number="241">
    <investment number="29">
        <rateLevel number="2">
            <ticker>ttt</ticker>
            <Cusip>1234</Cusip>
            <secId>ffff</secId>
            <investmentName>google</investmentName>
            <marketingStatusCode/>
            <marketingStatusReason/>
            <tiers>
                <tier>R6</tier>
            </tiers>
        </rateLevel>
    </investment>
    <investment number="28">
        <rateLevel number="3">
            <ticker>bbb</ticker>
            <Cusip>233</Cusip>
            <secId>tiee</secId>
            <investmentName>apple</investmentName>
            <marketingStatusCode/>
            <marketingStatusReason/>
            <tiers>
                <tier>R2</tier>
            </tiers>
        </rateLevel>
    </investment>
</product>
<product number="200">
    <investment number="8214">
        <rateLevel number="20">
            <ticker>yyyy</ticker>
            <Cusip>5655</Cusip>
            <secId>ghos</secId>
            <investmentName>microsof</investmentName>
            <marketingStatusCode/>
            <marketingStatusReason/>
            <tiers>
                <tier>Signature</tier>
            </tiers>
        </rateLevel>
    </investment>
</product>

我一直在使用group by但能够分组到rateLevel标签,但之后我得到了重复的标签。请帮忙。

提前致谢。

1 个答案:

答案 0 :(得分:0)

这是如何做到这一点的粗略的第一步。它当然可以重构。

XDocument xDoc = XDocument.Load(@"c:\temp\xmlproducts.xml");


var nodeGroup = from p in xDoc.Element("products").Descendants("product")
        group p by p.Attribute("number").Value into g
        select new 
        {
            Key = g.Key,
            Nodes = g.Descendants("investment")

        };


XDocument outDoc = new XDocument();

var root = new XElement("products");

nodeGroup.ToList().ForEach(grp=>
        {
            var product = new XElement("product");
            product.SetAttributeValue("number", grp.Key);

            product.Add(grp.Nodes);

            root.Add(product);
        });