user1570958
user1570958

Reputation:

how to merge child nodes of identical Id node using LINQ in XML

<Description>116423<ul><li>Creation Story</li></ul></Description>
<Description>116423<ul><li>Promise</li></ul></Description>
<Description>116423<ul><li>My House Is the Red Earth</li></ul></Description>   <Description>116423<ul><li>Letter From the End of the Twentieth Century</li></ul></Description>
<Description>116423<ul><li>Fear Poem     </li></ul></Description>
<Description>116423<ul><li>The Real Revolution is Love</li></ul></Description><Description>116423<ul><li>For Anna Mae Pictou Aquash</li></ul></Description><Description>116423<ul><li>She Had Some Horses </li></ul></Description><Description>116423<ul><li>The Myth of Blackbirds</li></ul></Description>
<Description>116423<ul><li>A Postcolonial Tale </li></ul></Description>

This is my XML and you can see that description node contain same Id than others but li tag contain different values of all. Now I want to merge all li nodes in a single description and ul node like this :

    <description>
    <ul>
               <li>Creation Story</li>
    <li>Promise</li>
    <li>My House Is the Red Earth</li>
    <li>Letter From the End of the Twentieth Century</li>
    <li>Fear Poem</li>
    <li>The Real Revolution is Love </li>
    <li>For Anna Mae Pictou Aquash </li>                                                                                                                                                                                                                                     
    <li>She Had Some Horses </li>                                                                                                                                                                                                                                            
    <li>The Myth of Blackbirds  </li>                                                                                                                                                                                                                                        
    <li>A Postcolonial Tale </li>
    </ul>
</description>

Upvotes: 0

Views: 559

Answers (3)

Moha Dehghan
Moha Dehghan

Reputation: 18463

I guess you need to also group the nodes by the id:

var root = XElement.Parse(xml);

var newRoot = new XElement("root",
    from d in root.Descendants("Description")
    group d by ((XText)d.FirstNode).Value into g
    select new XElement("Description",
        new XElement("ul", g.Descendants("li"))));

Upvotes: 1

Zev Spitz
Zev Spitz

Reputation: 15327

var xml = @"
<root>
<Description>116423<ul><li>Creation Story</li></ul></Description>
<Description>116423<ul><li>Promise</li></ul></Description>
<Description>116423<ul><li>My House Is the Red Earth</li></ul></Description>   <Description>116423<ul><li>Letter From the End of the Twentieth Century</li></ul></Description>
<Description>116423<ul><li>Fear Poem     </li></ul></Description>
<Description>116423<ul><li>The Real Revolution is Love</li></ul></Description><Description>116423<ul><li>For Anna Mae Pictou Aquash</li></ul></Description><Description>116423<ul><li>She Had Some Horses </li></ul></Description><Description>116423<ul><li>The Myth of Blackbirds</li></ul></Description>
<Description>116423<ul><li>A Postcolonial Tale </li></ul></Description>
</root>
";
var elem=XElement.Parse(xml);

var newElem=new XElement("Description", 116423, 
    new XElement("ul",
        from li in elem.Descendants("li") select li
    )
);

Upvotes: 1

Davut G&#252;rb&#252;z
Davut G&#252;rb&#252;z

Reputation: 5716

    XDocument xdoc = XDocument.Load(@"XMLFile1.xml");
    var modified = new XElement("Description", new XElement("ul", xdoc.Elements("Description").Elements("ul").Elements("li")));

Here is the result :)

<Description>
  <ul>
    <li>Creation Story</li>
    <li>Promise</li>
    <li>My House Is the Red Earth</li>
    <li>Letter From the End of the Twentieth Century</li>
    <li>Fear Poem     </li>
    <li>The Real Revolution is Love</li>
    <li>For Anna Mae Pictou Aquash</li>
    <li>She Had Some Horses </li>
    <li>The Myth of Blackbirds</li>
    <li>A Postcolonial Tale </li>
  </ul>
</Description>

Upvotes: 0

Related Questions