Example1.cs

Go to the documentation of this file.
00001 
00002 //
00003 // XML storage classes version 1.5
00004 //
00005 // Example1.cs
00006 //
00007 // Copyright (c) 2010 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 XMLStorage;
00041 
00042 /**
00043  * Example for writing and reading a XML file.
00044  * 
00045  * @author martin
00046  */
00047 public class Example1
00048 {
00049     /**
00050      * Write exampe XML file.
00051      */
00052     public void testWrite()
00053     {
00054         XMLDoc doc = new XMLDoc();
00055         XMLPos pos = new XMLPos(doc);
00056 
00057          // Zusammenstellen der XML-Struktur im Dokument 'doc'
00058         pos.create("PARAMS");
00059           pos.create("TEXT");
00060             pos.setContent("Testgruppe\nZeile 2", true);
00061           pos.back(); // </TEXT>
00062 
00063           pos.create("COLOR");
00064             pos.create("RED");
00065             pos.setContent("234");
00066             pos.back(); // </RED>
00067 
00068             pos.create("GREEN");
00069             pos.setContent("22");
00070             pos.back(); // </GREEN>
00071 
00072             pos.create("BLUE");
00073             pos.setContent("33");
00074             pos.back(); // </BLUE>
00075           pos.back(); // </COLOR>
00076 
00077           pos.create("FONT");
00078             pos.create("FONT_FILENAME");
00079             pos.setContent("Arial.ttf");
00080             pos.back(); // </FONT_FILENAME>
00081 
00082             pos.create("SIZE");
00083             pos.setContent("12");
00084             pos.back(); // </SIZE>
00085           pos.back(); // </FONT>
00086         pos.back(); // </PARAMS>
00087 
00088          // Schreiben der XML-Datei
00089         doc.writeFile("example1.xml");
00090     }
00091 
00092 
00093     /**
00094      * Read the example file back again.
00095      */
00096     public void testRead()
00097     {
00098         XMLDoc doc = new XMLDoc();
00099 
00100          // Einlesen des XML-Files
00101         doc.readFile("example1.xml");
00102 
00103         string txt = "";
00104         string filename = "";
00105 
00106         XMLPos pos = new XMLPos(doc);
00107 
00108         if (pos.goDown("PARAMS")) {
00109             if (pos.goDown("TEXT")) {
00110                 txt = pos.getContent();
00111                 pos.back();
00112             }
00113             pos.back();
00114         }
00115 
00116          // Abfrage eines Elementinhalts, beispielsweise von FONT_FILENAME
00117         if (pos.go("PARAMS/FONT/FONT_FILENAME")) {
00118             filename = pos.getContent();
00119             pos.back();
00120         }
00121 
00122          // Ausgabe der ermittelten Werte
00123         System.Console.Out.WriteLine("TEXT: " + txt);
00124         System.Console.Out.WriteLine("font filename: " + filename);
00125 
00126 //      assertEquals("Arial.ttf", filename); 
00127     }
00128 
00129 
00130     /**
00131      * Main function of Example1.
00132      * @param args unused command line arguments
00133      */
00134     static void Main(string[] args)
00135     {
00136         Example1 me = new Example1();
00137 
00138         try {
00139             me.testWrite();
00140             me.testRead();
00141         } catch(Exception e) {
00142             System.Console.Error.WriteLine(e);
00143             System.Console.Error.WriteLine(e.StackTrace);
00144         }
00145     }
00146 }