Beispiel #1
0
        private void LoadDocument(string file, string location)
        {
            using (var stream = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                _document = XDocument.Load(stream, LoadOptions.SetLineInfo);
            }

            if (Root == null)
            {
                return;
            }

            var      nodes = Root.Nodes();
            XElement first = null;

            foreach (var node in nodes)
            {
                if (node is XComment)
                {
                    continue;
                }

                if (node is XElement element)
                {
                    first = first ?? element;
                    var tag = element.Name.LocalName;
                    if (tag == "configSections")
                    {
                        if (first != element)
                        {
                            // IMPORTANT: double spaces before It to match IIS.
                            throw new COMException($"Line number: {(element as IXmlLineInfo).LineNumber}\r\nError: Only one <configSections> element allowed.  It must be the first child element of the root <configuration> element   \r\n");
                        }

                        RootSectionGroup.ParseSectionDefinitions(element, _sectionSchemas);
                        if (Parent == null)
                        {
                            // machine.config
                            RootSectionGroup.AddSpecial();
                        }

                        // TODO: can we use _sectionSchemas.
                        RootSectionGroup.GetAllDefinitions(DefinitionCache);
                        continue;
                    }

                    if (tag == "location")
                    {
                        // IMPORTANT: allow null path due to root web.config.
                        var path  = element.Attribute("path")?.Value;
                        var mode  = element.Attribute("overrideMode").LoadString("Inherit");
                        var found = Locations.FirstOrDefault(item => item.Path == path);
                        if (found == null)
                        {
                            found = new Location(path, mode, element, true);
                            Locations.Add(found);
                        }

                        ParseSections(element, null, found);
                        continue;
                    }

                    if (location == null)
                    {
                        ParseSections(element, null, null);
                    }
                    else
                    {
                        var found = Locations.FirstOrDefault(item => item.Path == location);
                        if (found == null)
                        {
                            found = new Location(location, "Inherit", element);
                            Locations.Add(found);
                        }

                        ParseSections(element, null, found);
                    }
                }
            }
        }
Beispiel #2
0
        private void LoadDocument(string file, string location)
        {
            _document = XDocument.Load(file, LoadOptions.SetLineInfo);
            if (Root == null)
            {
                return;
            }

            var nodes = Root.Nodes();

            foreach (var node in nodes)
            {
                if (node is XComment)
                {
                    continue;
                }

                var element = node as XElement;
                if (element != null)
                {
                    var tag = element.Name.LocalName;
                    if (tag == "configSections")
                    {
                        RootSectionGroup.ParseSectionDefinitions(element, _sectionSchemas);
                        continue;
                    }

                    if (tag == "location")
                    {
                        // IMPORTANT: allow null path due to root web.config.
                        var path  = element.Attribute("path")?.Value;
                        var mode  = element.Attribute("overrideMode").LoadString("Inherit");
                        var found = Locations.FirstOrDefault(item => item.Path == path);
                        if (found == null)
                        {
                            found = new Location(path, mode, element, true);
                            Locations.Add(found);
                        }

                        ParseSections(element, null, found);
                        continue;
                    }

                    if (location == null)
                    {
                        ParseSections(element, null, null);
                    }
                    else
                    {
                        var found = Locations.FirstOrDefault(item => item.Path == location);
                        if (found == null)
                        {
                            found = new Location(location, "Inherit", element);
                            Locations.Add(found);
                        }

                        ParseSections(element, null, found);
                    }
                }
            }
        }