Prevents an Empty Element : XElement : XML LINQ C# Source Code


Custom Search

C# Source Code » XML LINQ » XElement »

 

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

    }
}

 
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo XML LINQ
» XElement