Mega Code Archive

 
Categories / C# / XML LINQ
 

XDocument Save Method (XmlWriter) serializes this XDocument to an XmlWriter

using System; using System.IO; using System.Text; using System.Linq; using System.Xml.Linq; using System.Xml; using System.Collections; using System.Collections.Generic; public class MainClass {     public static void Main()     {         StringBuilder sb = new StringBuilder();         XmlWriterSettings xws = new XmlWriterSettings();         xws.OmitXmlDeclaration = true;         xws.Indent = true;         using (XmlWriter xw = XmlWriter.Create(sb, xws))         {             XDocument doc = new XDocument(                 new XElement("Child",                     new XElement("GrandChild", "some content")                 )             );             doc.Save(xw);         }         Console.WriteLine(sb.ToString());     } }