• Main Page
  • Namespaces
  • Classes
  • Files
  • Directories
  • File List

XMLDoc.cs

Go to the documentation of this file.
00001 
00002 //
00003 // XML storage classes version 1.5
00004 //
00005 // XMLDoc.cs
00006 //
00007 // Copyright (c) 2010, 2014 Martin Fuchs <martin-fuchs@gmx.net>
00008 //
00009 
00010 
00011 /*
00012 
00013  All rights reserved.
00014 
00015  Redistribution and use in source and binary forms, with or without
00016  modification, are permitted provided that the following conditions are met:
00017 
00018  * Redistributions of source code must retain the above copyright
00019     notice, this list of conditions and the following disclaimer.
00020  * Redistributions in binary form must reproduce the above copyright
00021     notice, this list of conditions and the following disclaimer in
00022     the documentation and/or other materials provided with the
00023     distribution.
00024 
00025  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00028  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00029  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00030  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00031  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00032  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00033  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00034  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00035  POSSIBILITY OF SUCH DAMAGE.
00036 
00037 */
00038 
00039 using System;
00040 using System.IO;
00041 using System.Xml;
00042 using System.Text;
00043 
00044 namespace XMLStorage
00045 {
00046 
00047 /**
00048  * XMLDoc works as XML document holder.
00049  */
00050 public class XMLDoc : XMLNode
00051 {
00052     public XMLHeader _header = new XMLHeader();
00053 
00054     public XMLDoc()
00055      :  base("")
00056     {
00057     }
00058 
00059     public XMLDoc(string path, XmlReaderSettings xmlSettings=null)
00060      :  base("")
00061     {
00062         readFile(path, xmlSettings);
00063     }
00064 
00065     public void readFile(string path, XmlReaderSettings xmlSettings=null)
00066     {
00067         read(new StreamReader(path), xmlSettings);
00068     }
00069 
00070     public void read(TextReader reader, XmlReaderSettings xmlSettings=null)
00071     {
00072         XMLDocReader docReader = new XMLDocReader(this);
00073 
00074         docReader.read(reader, xmlSettings);
00075 
00076         _header._additional = docReader.getInstructions();
00077     }
00078 
00079      /// write XML stream preserving previous white space and comments
00080     public override void write(TextWriter w)
00081     {
00082         write(w, WRITE_MODE.FORMAT_SMART);
00083     }
00084 
00085      /// write XML stream
00086      // FORMAT_SMART: preserving previous white space and comments
00087     public override void write(TextWriter w, WRITE_MODE mode/*=FORMAT_SMART*/)
00088     {
00089         _header.print(w);//TODO, mode!=FORMAT_PLAIN
00090 
00091         if (_children.Count == 1)
00092             _children.First.Value.write(w, mode);
00093         else if (_children.Count > 0)
00094             throw new Exception("more than one XML root entry");
00095     }
00096 
00097      /// write XML stream with formating
00098     public void writeFormating(TextWriter w)
00099     {
00100         write(w, WRITE_MODE.FORMAT_PRETTY);
00101     }
00102 
00103     public void writeFile(string path)
00104     {
00105         writeFile(path, WRITE_MODE.FORMAT_SMART);
00106     }
00107 
00108     public void writeFile(string path, WRITE_MODE mode/*=FORMAT_SMART*/)
00109     {
00110         string encoding = _header._encoding.Length>0? _header._encoding: "UTF-8";
00111 
00112         TextWriter w = new StreamWriter(path, false, Encoding.GetEncoding(encoding));
00113 
00114         try {
00115             write(w, mode);
00116         } finally {
00117             w.Close();
00118         }
00119     }
00120 
00121     public void writeFormating(string path)
00122     {
00123         TextWriter w = new StreamWriter(path, false, Encoding.UTF8);
00124 
00125         try {
00126             writeFormating(w);
00127         } finally {
00128             w.Close();
00129         }
00130     }
00131 }
00132 
00133 } // namespace XMLStorage

Generated on Tue Oct 28 2014 00:05:07 for XMLStorage/C# by  doxygen 1.7.2