Prevents an Empty Element
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
public class MainClass {
public static void Main() {
IEnumerable<XElement> elements =
new XElement[] {
new XElement("Book",
new XElement("Name", "J"),
new XElement("Book", "LINQ")),
new XElement("Book",
new XElement("Name", "J"))};
XElement xElement =
new XElement("Books",
elements.Select(e =>
new XElement(e.Name,
new XElement(e.Element("Name").Name, e.Element("Name").Value),
e.Elements("Book").Any() ?
new XElement("Books", e.Elements("Book")) : null)));
Console.WriteLine(xElement);
}
}
Related examples in the same category