Advertisement
cansik

Simple_XmlWriter

Dec 7th, 2011
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace System.Xml
  8. {
  9.     public class XmlTextWriter
  10.     {
  11.         /// <summary>
  12.         /// Internal writer object
  13.         /// </summary>
  14.         internal StringWriter XmlDoc { get; set; }
  15.  
  16.         /// <summary>
  17.         /// internal storage for current opened element
  18.         /// </summary>
  19.         private Stack<string> openElements;
  20.  
  21.         public XmlTextWriter()
  22.         {
  23.             if (this.XmlDoc == null)
  24.             {
  25.                 this.XmlDoc = new StringWriter();
  26.             }
  27.  
  28.             openElements = new Stack<string>();
  29.         }
  30.  
  31.         public XmlTextWriter(StringWriter writer) : this()
  32.         {
  33.             this.XmlDoc = writer;
  34.         }
  35.  
  36.         /// <summary>
  37.         /// Writes the header of the xml
  38.         /// </summary>
  39.         public void WriteStartDocument()
  40.         {
  41.             this.WriteStartDocument(false);
  42.         }
  43.  
  44.         /// <summary>
  45.         /// Writes the header of the xml
  46.         /// </summary>
  47.         /// <param name="standalone">defines if standalone or not</param>
  48.         public void WriteStartDocument(bool standalone)
  49.         {
  50.             string standaloneText = "no";
  51.  
  52.             if (standalone)
  53.             {
  54.                 standaloneText = "yes";
  55.             }
  56.  
  57.             this.XmlDoc.Write("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"" + standaloneText + "\"?>");
  58.         }
  59.  
  60.         /// <summary>
  61.         /// Writes a raw string into the document
  62.         /// </summary>
  63.         /// <param name="data">Data to write</param>
  64.         public void WriteRaw(string data)
  65.         {
  66.             this.XmlDoc.Write(data);
  67.         }
  68.  
  69.         /// <summary>
  70.         /// Open a new XmlElement
  71.         /// </summary>
  72.         /// <param name="elementName">Name of the Element</param>
  73.         public void WriteStartElement(string elementName)
  74.         {
  75.             this.XmlDoc.Write("<" + elementName + ">");
  76.             openElements.Push(elementName);
  77.         }
  78.  
  79.         /// <summary>
  80.         /// Close an XmlElement
  81.         /// </summary>
  82.         public void WriteEndElement()
  83.         {
  84.             this.XmlDoc.Write("</" + openElements.Pop() + ">");
  85.         }
  86.  
  87.         /// <summary>
  88.         /// Writes an XmlElement string
  89.         /// </summary>
  90.         /// <param name="elementName">Name of the Element</param>
  91.         /// <param name="Data">Data to write</param>
  92.         public void WriteElementString(string elementName, string Data)
  93.         {
  94.             this.XmlDoc.Write("<" + elementName + ">");
  95.             this.XmlDoc.Write(Data);
  96.             this.XmlDoc.Write("</" + elementName + ">");
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement