0

I have an XML file

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

and would like to reformat/reorganize the (product) tags (using Linq) to

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

I have been using group by but am able to group to rateLevel tag but after that I get repeated tags. Please help.

Thanks in advance.

4

1 回答 1

0

This is a rough first cut at how to do it. It could certainly be refactored.

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);
        });
于 2012-04-21T01:02:00.220 回答