public void Merge ()
		{
			StringWriter textWriter = new StringWriter ();
			XmlTextWriter xmlWriter = NiniWriter (textWriter);
			WriteSection (xmlWriter, "Pets");
			WriteKey (xmlWriter, "cat", "muffy");
			WriteKey (xmlWriter, "dog", "rover");
			WriteKey (xmlWriter, "bird", "tweety");
			xmlWriter.WriteEndDocument ();
			
			StringReader reader = new StringReader (textWriter.ToString ());
			XmlTextReader xmlReader = new XmlTextReader (reader);
			XmlConfigSource xmlSource = new XmlConfigSource (xmlReader);
			
			StringWriter writer = new StringWriter ();
			writer.WriteLine ("[People]");
			writer.WriteLine (" woman = Jane");
			writer.WriteLine (" man = John");
			IniConfigSource iniSource = 
					new IniConfigSource (new StringReader (writer.ToString ()));
			
			xmlSource.Merge (iniSource);
			
			IConfig config = xmlSource.Configs["Pets"];
			Assert.AreEqual (3, config.GetKeys ().Length);
			Assert.AreEqual ("muffy", config.Get ("cat"));
			Assert.AreEqual ("rover", config.Get ("dog"));
			
			config = xmlSource.Configs["People"];
			Assert.AreEqual (2, config.GetKeys ().Length);
			Assert.AreEqual ("Jane", config.Get ("woman"));
			Assert.AreEqual ("John", config.Get ("man"));
		}
示例#2
0
 /// <summary>
 /// Saves the peer configuration.
 /// </summary>
 /// <param name="fileName">The name of the file where to save it.</param>
 private void SaveTo(String fileName)
 {
     // Save the configuration as input for future reconfiguration
     try
     {
         using (FileStream oStream = new FileStream(fileName, FileMode.Create))
         {
             XmlConfigSource source2 = new XmlConfigSource();
             source2.Merge(source);
             source2.Save(oStream);
         }
     }
     catch (Exception e)
     {
         if (log.IsWarnEnabled)
         {
             log.Warn(e);
             log.Warn("Could not save to " + fileName);
         }
     }
 }
示例#3
0
        public void MergeAndSave()
        {
            string xmlFileName = "NiniConfig.xml";

            StreamWriter textWriter = new StreamWriter (xmlFileName);
            XmlTextWriter xmlWriter = NiniWriter (textWriter);
            WriteSection (xmlWriter, "Pets");
            WriteKey (xmlWriter, "cat", "Muffy");
            WriteKey (xmlWriter, "dog", "Rover");
            WriteKey (xmlWriter, "bird", "Tweety");
            xmlWriter.WriteEndDocument ();
            xmlWriter.Close ();

            StringWriter writer = new StringWriter ();
            writer.WriteLine ("[Pets]");
            writer.WriteLine ("cat = Becky"); // overwrite
            writer.WriteLine ("lizard = Saurus"); // new
            writer.WriteLine ("[People]");
            writer.WriteLine (" woman = Jane");
            writer.WriteLine (" man = John");
            IniConfigSource iniSource = new IniConfigSource
                                    (new StringReader (writer.ToString ()));

            XmlConfigSource xmlSource = new XmlConfigSource (xmlFileName);

            xmlSource.Merge (iniSource);

            IConfig config = xmlSource.Configs["Pets"];
            Assert.AreEqual (4, config.GetKeys ().Length);
            Assert.AreEqual ("Becky", config.Get ("cat"));
            Assert.AreEqual ("Rover", config.Get ("dog"));
            Assert.AreEqual ("Saurus", config.Get ("lizard"));

            config = xmlSource.Configs["People"];
            Assert.AreEqual (2, config.GetKeys ().Length);
            Assert.AreEqual ("Jane", config.Get ("woman"));
            Assert.AreEqual ("John", config.Get ("man"));

            config.Set ("woman", "Tara");
            config.Set ("man", "Quentin");

            xmlSource.Save ();

            xmlSource = new XmlConfigSource (xmlFileName);

            config = xmlSource.Configs["Pets"];
            Assert.AreEqual (4, config.GetKeys ().Length);
            Assert.AreEqual ("Becky", config.Get ("cat"));
            Assert.AreEqual ("Rover", config.Get ("dog"));
            Assert.AreEqual ("Saurus", config.Get ("lizard"));

            config = xmlSource.Configs["People"];
            Assert.AreEqual (2, config.GetKeys ().Length);
            Assert.AreEqual ("Tara", config.Get ("woman"));
            Assert.AreEqual ("Quentin", config.Get ("man"));

            File.Delete  (xmlFileName);
        }
示例#4
0
        public void MergeExisting()
        {
            StringWriter textWriter = new StringWriter ();
            XmlTextWriter xmlWriter = NiniWriter (textWriter);
            WriteSection (xmlWriter, "Pets");
            WriteKey (xmlWriter, "cat", "muffy");
            xmlWriter.WriteEndDocument ();

            StringReader reader = new StringReader (xmlWriter.ToString ());
            XmlTextReader xmlReader = new XmlTextReader (reader);
            XmlConfigSource xmlSource = new XmlConfigSource (xmlReader);

            StringWriter writer = new StringWriter ();
            writer.WriteLine ("[People]");
            writer.WriteLine (" woman = Jane");
            IniConfigSource iniSource =
                    new IniConfigSource (new StringReader (writer.ToString ()));

            xmlSource.Merge (iniSource);
            xmlSource.Merge (iniSource); // exception
        }
示例#5
0
        protected void RecordConfiguration(IConfigSource originalConfig)
        {
            m_xtw.WriteStartElement(ConfigElement);

            // Write out game configuration
            // We have to transfer this to an xml document to get xml
            XmlConfigSource intermediateConfig = new XmlConfigSource();
            intermediateConfig.Merge(originalConfig);

            // This is a roundabout way to get rid of the top xml processing directive that config.ToString() gives
            // us
            XmlDocument outputDoc = new XmlDocument();
            outputDoc.LoadXml(intermediateConfig.ToString());

            // Remove the other document's processing instruction
            outputDoc.RemoveChild(outputDoc.FirstChild);

            outputDoc.WriteTo(m_xtw);

            m_xtw.WriteStartElement(RegionsElement);
            // Write region names, positions and IDs
            foreach (Scene scene in m_controller.Scenes)
            {
                m_xtw.WriteStartElement(RegionElement);
                m_xtw.WriteAttributeString("Name", scene.RegionInfo.RegionName);
                m_xtw.WriteAttributeString("X", scene.RegionInfo.RegionLocX.ToString());
                m_xtw.WriteAttributeString("Y", scene.RegionInfo.RegionLocY.ToString());
                m_xtw.WriteEndElement();
            }
            m_xtw.WriteEndElement();

            m_xtw.WriteEndElement();
        }