如何使用XML中的LINQ合并相同Id节点的子节点

时间:2013-03-29 07:49:12

标签: c# linq-to-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>

这是我的XML,您可以看到描述节点包含的ID与其他节点相同,但li标签包含不同的所有值。 现在我想在单个描述和ul节点中合并所有li节点,如下所示:

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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

我猜你还需要用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"))));

答案 2 :(得分:0)

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

结果如下:)

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