Example #1
0
        public object Create(object parent, object configContext, XmlNode section)
        {
            XPathNavigator navigator = section.CreateNavigator();

            string path = (string)navigator.Evaluate("string(@path)");

            if (path != null && path.Length > 0)
            {
                bool envFriendly = false;
                try
                {
                    envFriendly = Boolean.Parse((string)navigator.Evaluate("string(@envFriendly)"));
                }
                catch { }

                if (envFriendly)
                {
                    path = ChoEnvironmentSettings.ToEnvSpecificConfigFile(path);
                }

                path = ChoPath.GetFullPath(path);
                if (!File.Exists(path))
                {
                    throw new ApplicationException(String.Format(CultureInfo.InvariantCulture, Resources.ES1002, path));
                }

                XmlDocument document = new XmlDocument();
                document.Load(path);

                section   = document.DocumentElement;
                navigator = section.CreateNavigator();
            }

            string typename = (string)navigator.Evaluate("string(@type)");

            if (typename == null || typename.Trim().Length == 0)
            {
                throw new ApplicationException(String.Format(CultureInfo.InvariantCulture, Resources.ES1003, section.Name));
            }

            Type type = Type.GetType(typename);

            if (type == null)
            {
                type = ChoType.GetType(typename);
            }

            if (type == null)
            {
                throw new ApplicationException(String.Format(CultureInfo.InvariantCulture, Resources.ES1004, typename));
            }

            ChoConfigFilesMapper.Add(section.Name, path);

            XmlSerializer serializer = new XmlSerializer(type);

            try
            {
                return(serializer.Deserialize(new XmlNodeReader(section)));
            }
            catch
            {
                if (section.Name != type.Name && section.Name.ToUpper() == type.Name.ToUpper())
                {
                    XmlNode newSection = new XmlDocument().CreateElement(type.Name);
                    newSection.InnerXml = section.InnerXml;
                    section             = newSection;
                }
                return(serializer.Deserialize(new XmlNodeReader(section)));
            }
        }
Example #2
0
        public object Create(object parent, object configContext, XmlNode section)
        {
            XPathNavigator navigator = section.CreateNavigator();

            string path = (string)navigator.Evaluate("string(@path)");

            if (path != null && path.Length > 0)
            {
                bool envFriendly = false;
                try
                {
                    envFriendly = Boolean.Parse((string)navigator.Evaluate("string(@envFriendly)"));
                }
                catch { }

                if (envFriendly)
                {
                    path = ChoEnvironmentSettings.ToEnvSpecificConfigFile(path);
                }

                path = RITPath.GetFullPath(path);
                if (!File.Exists(path))
                {
                    throw new ApplicationException(String.Format("{0} not exists.", path));
                }

                XmlDocument document = new XmlDocument();
                document.Load(path);

                section   = document.DocumentElement;
                navigator = section.CreateNavigator();
            }

            string typename = (string)navigator.Evaluate("string(@type)");

            if (typename == null || typename.Trim().Length == 0)
            {
                throw new ApplicationException(String.Format("Missing type attribute in the '{0}' config section.", section.Name));
            }

            Type type = Type.GetType(typename);

            if (type == null)
            {
                type = new RITType().GetType(typename);
            }

            if (type == null)
            {
                throw new ApplicationException(String.Format("Can't find {0} type.", typename));
            }

            RITConfigFileLocator.Add(section.Name, path);

            XmlSerializer serializer = new XmlSerializer(type);

            try
            {
                return(serializer.Deserialize(new XmlNodeReader(section)));
            }
            catch
            {
                if (section.Name != type.Name && section.Name.ToUpper() == type.Name.ToUpper())
                {
                    XmlNode newSection = new XmlDocument().CreateElement(type.Name);
                    newSection.InnerXml = section.InnerXml;
                    section             = newSection;
                }
                return(serializer.Deserialize(new XmlNodeReader(section)));
            }
        }