Save XmlDocument to TextWriter - CSharp System.Xml

CSharp examples for System.Xml:XML Document

Description

Save XmlDocument to TextWriter

Demo Code


using System.Globalization;
using System.Security.Cryptography;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Collections;
using System.Xml.XPath;
using System.Text;
using System.Xml;
using System.IO;/* ww w. j a v a2s . c o m*/
using System;

public class Main{
        /// <summary>
      /// 
      /// </summary>
      /// <param name="Document"></param>
      /// <param name="writer"></param>
      public static void Display(XmlDocument Document, TextWriter writer)
      {
         MemoryStream str = new MemoryStream();
         XmlTextWriter tw = new XmlTextWriter(str,
            Encoding.GetEncoding(((XmlDeclaration) Document.FirstChild).Encoding));
         tw.Formatting = Formatting.Indented;
         tw.Indentation = 2;
         Document.Save(tw);
         tw.Flush();

         str.Position = 0;
         StreamReader sr = new StreamReader(str);
         writer.Write(sr.ReadToEnd());
      }
}

Related Tutorials