0
<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 标记包含所有不同的值。现在我想将所有 li 节点合并到一个描述和 ul 节点中,如下所示:

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

3 回答 3

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
    )
);
于 2013-03-29T08:35:22.753 回答
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"))));
于 2013-03-29T09:47:01.660 回答
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>
于 2013-03-29T08:44:16.853 回答