1 // ****************************************************************
\r
2 // Copyright 2007, Charlie Poole
\r
3 // This is free software licensed under the NUnit license. You may
\r
4 // obtain a copy of the license at http://nunit.org/?p=license&r=2.4
\r
5 // ****************************************************************
\r
10 using System.Collections;
\r
11 using System.ComponentModel;
\r
13 namespace NUnit.Util
\r
16 /// Summary description for XmlSettingsStorage.
\r
18 public class XmlSettingsStorage : MemorySettingsStorage
\r
20 private string filePath;
\r
22 public XmlSettingsStorage( string filePath )
\r
24 this.filePath = filePath;
\r
27 public override void LoadSettings()
\r
29 FileInfo info = new FileInfo(filePath);
\r
30 if ( !info.Exists || info.Length == 0 )
\r
35 XmlDocument doc = new XmlDocument();
\r
36 doc.Load( filePath );
\r
38 foreach( XmlElement element in doc.DocumentElement["Settings"].ChildNodes )
\r
40 if ( element.Name != "Setting" )
\r
41 throw new ApplicationException( "Unknown element in settings file: " + element.Name );
\r
43 if ( !element.HasAttribute( "name" ) )
\r
44 throw new ApplicationException( "Setting must have 'name' attribute" );
\r
46 if ( !element.HasAttribute( "value" ) )
\r
47 throw new ApplicationException( "Setting must have 'value' attribute" );
\r
49 settings[ element.GetAttribute( "name" ) ] = element.GetAttribute( "value" );
\r
52 catch( Exception ex )
\r
54 throw new ApplicationException( "Error loading settings file", ex );
\r
58 public override void SaveSettings()
\r
60 string dirPath = Path.GetDirectoryName( filePath );
\r
61 if ( !Directory.Exists( dirPath ) )
\r
62 Directory.CreateDirectory( dirPath );
\r
64 XmlTextWriter writer = new XmlTextWriter( filePath, System.Text.Encoding.UTF8 );
\r
65 writer.Formatting = Formatting.Indented;
\r
67 writer.WriteProcessingInstruction( "xml", "version=\"1.0\"" );
\r
68 writer.WriteStartElement( "NUnitSettings" );
\r
69 writer.WriteStartElement( "Settings" );
\r
71 ArrayList keys = new ArrayList( settings.Keys );
\r
74 foreach( string name in keys )
\r
76 object val = settings[name];
\r
79 writer.WriteStartElement( "Setting");
\r
80 writer.WriteAttributeString( "name", name );
\r
81 writer.WriteAttributeString( "value", val.ToString() );
\r
82 writer.WriteEndElement();
\r
86 writer.WriteEndElement();
\r
87 writer.WriteEndElement();
\r