Mega Code Archive

 
Categories / C# / XML LINQ
 

Passing XComment and XElement to XDocument

using System; using System.Linq; using System.Xml.Linq; using System.Collections; using System.Collections.Generic; public class MainClass{    public static void Main(){         XDocument srcTree = new XDocument(             new XComment("This is a comment"),             new XElement("Root",                 new XElement("Child1", "data1"),                 new XElement("Child2", "data2"),                 new XElement("Child3", "data3")             )         );                  XDocument doc = new XDocument(             new XComment("This is a comment"),             new XElement("Root",                 from el in srcTree.Element("Root").Elements()                 where ((string)el).StartsWith("data")                 select el             )         );         Console.WriteLine(doc);     } }