示例#1
0
 public static HttpCloneConfig ReadConfig(Uri site, string directory)
 {
     if (Directory.Exists(directory))
     {
         string test = Path.Combine(directory, "HttpClone.config");
         if (File.Exists(test))
         {
             using (XmlReader rdr = new XmlTextReader(test))
                 return(ReadXml(SCHEMA_NAME, rdr));
         }
     }
     return(XmlConfiguration <HttpCloneConfig> .ReadConfig("HttpCloneConfig"));
 }
示例#2
0
        public void Test()
        {
            if (File.Exists(Section1.XsdFile))
            {
                File.Delete(Section1.XsdFile);
            }

            Section1 a = (XmlConfiguration <Section1>)ConfigurationManager.GetSection("Section1");

            Assert.AreEqual(123, a.id);
            Assert.AreEqual(432.1, a.value);
            Assert.AreEqual("hello", a.name);
            a = null;

            Section2 b = ((XmlConfiguration <Section2>)ConfigurationManager.GetSection("Section2")).Settings;

            Assert.AreEqual(123, b.id);
            Assert.AreEqual("432.1", b.value);
            Assert.AreEqual("hello", b.name);
            b = null;

            b = XmlConfiguration <Section2> .ReadConfig("Section2");

            Assert.AreEqual(123, b.id);
            Assert.AreEqual("432.1", b.value);
            Assert.AreEqual("hello", b.name);
            b = null;

            using (XmlReader rdr = new XmlTextReader(new StringReader("<Section2 id='123' value='432.1'><name>hello</name></Section2>")))
            {
                b = XmlConfiguration <Section2> .ReadXml(rdr);

                Assert.AreEqual(123, b.id);
                Assert.AreEqual("432.1", b.value);
                Assert.AreEqual("hello", b.name);
                b = null;
            }

            XmlConfiguration <Section2> .XmlSchema = System.Xml.Schema.XmlSchema.Read(
                this.GetType().Assembly.GetManifestResourceStream(this.GetType().Namespace + ".TestXmlSection2.xsd"),
                null
                );

            //now we should get an exception when reading section 2...
            try
            {
                ConfigurationManager.RefreshSection("Section2");
                b = (XmlConfiguration <Section2>)ConfigurationManager.GetSection("Section2");
            }
            catch (System.Configuration.ConfigurationErrorsException ce)
            {
                Assert.IsTrue(ce.Message.Contains("The 'value' attribute is invalid"));
                Assert.IsNotNull(ce.InnerException);
                Assert.AreEqual(typeof(XmlException), ce.InnerException.GetType());
            }
            Assert.IsNull(b);

            Section3 c = (XmlConfiguration <Section3>)ConfigurationManager.GetSection("Section3");

            Assert.AreEqual(123, c.id);
            Assert.AreEqual(new DateTime(2009, 12, 25, 0, 0, 0), c.value);
            Assert.AreEqual("hello", c.name);
            b = null;

            // the following invalid xsd will cause an exception
            File.WriteAllText(Section1.XsdFile, "<xs:schema  xmlns:xs='http://www.w3.org/2001/XMLSchema'><xs:element /></xs:schema>");
            ConfigurationManager.RefreshSection("Section1");
            string origDir = Environment.CurrentDirectory;

            Environment.CurrentDirectory = Path.GetTempPath();
            try
            {
                a = (XmlConfiguration <Section1>)ConfigurationManager.GetSection("Section1");
            }
            catch (ConfigurationErrorsException) { }
            finally
            {
                Environment.CurrentDirectory = origDir;
                File.Delete(Section1.XsdFile);
            }
            Assert.IsNull(a);
        }