Пример #1
0
        private void Load()
        {
            _xmlDocument = new XmlDocument();

            if (IsXmlFile)
            {
                _xmlDocument.Load(_filePath);
            }
            else
            {
                _xmlDocument.LoadXml(_xml);
            }

            ChoXmlDocument.ExpandIncludes(_filePath, out _xmlDocument);

            Start();

            if (_xmlDocument != null)
            {
                _xmlDocument.NodeChanged   += new XmlNodeChangedEventHandler(XmlDocument_NodeChanged);
                _xmlDocument.NodeChanging  += new XmlNodeChangedEventHandler(XmlDocument_NodeChanging);
                _xmlDocument.NodeInserted  += new XmlNodeChangedEventHandler(XmlDocument_NodeInserted);
                _xmlDocument.NodeInserting += new XmlNodeChangedEventHandler(XmlDocument_NodeInserting);
                _xmlDocument.NodeRemoved   += new XmlNodeChangedEventHandler(XmlDocument_NodeRemoved);
                _xmlDocument.NodeRemoving  += new XmlNodeChangedEventHandler(XmlDocument_NodeRemoving);
            }

            DocumentLoaded = true;
        }
Пример #2
0
        internal ChoXmlDocument(string filePathOrXml, ChoXmlDocument parentXmlDocument)
            : this(filePathOrXml)
        {
            ////_syncRoot = parentXmlDocument.SyncRoot;
            //_ultimateParentXmlDocument = _parentXmlDocument = parentXmlDocument;

            //while (_ultimateParentXmlDocument.ParentXmlDocument != _ultimateParentXmlDocument)
            //    _ultimateParentXmlDocument = _ultimateParentXmlDocument.ParentXmlDocument;
        }
Пример #3
0
        private static string[] ExpandIncludes(XmlNode section, string baseDirectory, List <string> parentFileList)
        {
            if (!String.IsNullOrEmpty(baseDirectory) && !parentFileList.Contains(baseDirectory) && !String.IsNullOrEmpty(Path.GetFileName(baseDirectory)))
            {
                parentFileList.Add(baseDirectory);
            }

            if (!String.IsNullOrEmpty(baseDirectory))
            {
                baseDirectory = Path.GetDirectoryName(baseDirectory);
            }

            List <string> includeFileList = new List <string>();

            XmlNodeList includeNodeList = section.SelectNodes(String.Format("//include[@{0}]", PathToken));

            foreach (XmlElement element in includeNodeList)
            {
                string includeFileName = element.GetAttribute(PathToken);
                //if (String.IsNullOrEmpty(includeFileName)) continue;
                if (!String.IsNullOrEmpty(baseDirectory) && !Path.IsPathRooted(includeFileName))
                {
                    includeFileName = Path.Combine(baseDirectory, includeFileName);
                }

                if (!String.IsNullOrEmpty(baseDirectory) && IsCircularFileExists(parentFileList, includeFileName))
                {
                    throw new ChoXmlDocumentException(String.Format("Circular reference encountered on the {0} file.", baseDirectory));
                }

                if (!includeFileList.Contains(includeFileName))
                {
                    includeFileList.Add(includeFileName);
                }

                XmlDocument         ownerDocment = section is XmlDocument ? section as XmlDocument : section.OwnerDocument;
                XmlDocumentFragment fragment     = ownerDocment.CreateDocumentFragment();

                try
                {
                    XmlDocument includeDoc = ChoXmlDocument.Load(includeFileName);

                    foreach (XmlAttribute attr in element.Attributes)
                    {
                        if (attr.Name != PathToken)
                        {
                            includeDoc.DocumentElement.SetAttribute(attr.Name, attr.Value);
                        }
                    }

                    fragment.InnerXml = includeDoc.InnerXml;

                    XmlElement xmlSubElement = (XmlElement)fragment.SelectSingleNode("/*");

                    string[] innerIncludeFileList = ExpandIncludes(xmlSubElement, baseDirectory, parentFileList);
                    foreach (string innerIncludeFile in innerIncludeFileList)
                    {
                        if (!includeFileList.Contains(includeFileName))
                        {
                            includeFileList.Add(innerIncludeFile);
                        }
                    }

                    element.ParentNode.ReplaceChild(xmlSubElement, element);
                    xmlSubElement.ParentNode.InsertBefore(xmlSubElement.OwnerDocument.CreateComment(String.Format(String.Format("DO NOT REMOVE - BEGIN INCLUDE {0}", includeFileName))),
                                                          xmlSubElement);
                    xmlSubElement.ParentNode.AppendChild(xmlSubElement.OwnerDocument.CreateComment(String.Format(String.Format("DO NOT REMOVE - END INCLUDE {0}", includeFileName))));
                }
                catch (XmlException)
                {
                    try
                    {
                        using (StreamReader sr = File.OpenText(includeFileName))
                            fragment.InnerXml = sr.ReadToEnd();

                        XmlNode parentNode = element.ParentNode;

                        parentNode.RemoveChild(element);

                        bool    isFirstChild = true;
                        XmlNode lastChild    = null;
                        foreach (XmlNode xmlNode in fragment.SelectNodes("/*|//comment()"))
                        {
                            if (xmlNode == null)
                            {
                                continue;
                            }

                            if (xmlNode is XmlElement && xmlNode.Name == "include")
                            {
                                string[] innerIncludeFileList = ExpandIncludes(xmlNode, includeFileName, parentFileList); //, false);
                                foreach (string innerIncludeFile in innerIncludeFileList)
                                {
                                    if (!includeFileList.Contains(includeFileName))
                                    {
                                        includeFileList.Add(innerIncludeFile);
                                    }
                                }
                            }

                            parentNode.AppendChild(xmlNode);
                            if (isFirstChild)
                            {
                                parentNode.InsertBefore(parentNode.OwnerDocument.CreateComment(String.Format(String.Format("DO NOT REMOVE - BEGIN INCLUDE {0}", includeFileName))),
                                                        xmlNode);
                                isFirstChild = false;
                            }
                            lastChild = xmlNode;
                        }
                        parentNode.InsertAfter(parentNode.OwnerDocument.CreateComment(String.Format(String.Format("DO NOT REMOVE - END INCLUDE {0}", includeFileName))),
                                               lastChild);
                    }
                    catch (Exception innerEx)
                    {
                        throw new ChoXmlDocumentException(String.Format("{0}: Error loading xml file.", includeFileName), innerEx);
                    }
                }
                catch (Exception ex)
                {
                    throw new ChoXmlDocumentException(String.Format("{0}: Error loading xml file.", includeFileName), ex);
                }
            }

            return(includeFileList.ToArray());
        }