var xmlStr = @"<root>
<book>
<firstHalf></firstHalf>
<secondHalf></secondHalf>
</book>
</root>
";
var doc = XDocument.Parse(xmlStr);
// find all books
var books = doc.Descendants("book").ToList();
// p.s., need ToList() since we're modifying the structure
foreach (var book in books)
{
// get all the "extra" elements
var extraElements = book.Elements().Skip(1).ToList();
if (extraElements.Any())
{
// remove the extra elements from the current book
extraElements.Remove();
// create new books after this book with the extra element
book.AddAfterSelf(extraElements.Select(e => new XElement("book", e)));
}
}
产量:
<root>
<book>
<firstHalf></firstHalf>
</book>
<book>
<secondHalf></secondHalf>
</book>
</root>