Esempio n. 1
0
        /// <summary>
        /// Fill up given XContainer with data from Xml file.
        /// </summary>
        /// <param name="sourceContainer">Given XContainer to be filled up.</param>
        /// <param name="xmlDoc">Xml document contains data.</param>
        /// <returns>True if filling was successfull, false if not.</returns>
        private bool Fill(XContainer sourceContainer, XmlDocument xmlDoc)
        {
            var xmlNodeList = xmlDoc.GetElementsByTagName(sourceContainer.Name);

            if (xmlNodeList.Count > 0)
            {
                _newRoot = sourceContainer.Clone();
                _newRoot.Containers.Clear();
                ResolveAttributes(_newRoot, ((XmlElement)xmlNodeList[0]));
                ResolveElements(_newRoot, ((XmlElement)xmlNodeList[0]));
                _newRoot.Value = xmlNodeList[0].Value;
                _newRoot.Name  = xmlNodeList[0].Name;

                foreach (var element in xmlNodeList[0].ChildNodes)
                {
                    if (!(element is XmlComment))
                    {
                        if (element is XmlText)
                        {
                            _newRoot.Value = ((XmlText)element).Value;
                        }

                        foreach (var xContainer in sourceContainer.Containers)
                        {
                            var name = ((XmlElement)element).Name;
                            if (xContainer.Name == name)
                            {
                                FillChildNodes(_newRoot, xContainer, (XmlElement)element);
                            }
                        }
                    }
                }

                return(true);
            }

            return(false);
        }
Esempio n. 2
0
        /// <summary>
        /// Fill up child nodes of XContainer.
        /// </summary>
        /// <param name="destinationContainer">XContainer used to copy structure to source container.</param>
        /// <param name="sourceContainer">Source container contained data.</param>
        /// <param name="xmlElement">Xml Element contains data.</param>
        private void FillChildNodes(XContainer destinationContainer, XContainer sourceContainer, XmlElement xmlElement)
        {
            var newContainer = sourceContainer.Clone();

            newContainer.Containers.Clear();
            ResolveAttributes(newContainer, xmlElement);
            ResolveElements(newContainer, xmlElement);

            newContainer.Name = xmlElement.Name;

            if (xmlElement.ChildNodes.Count == 1)
            {
                if (xmlElement.ChildNodes[0] is XmlText)
                {
                    newContainer.Value = xmlElement.InnerText;
                }
            }

            newContainer.ParentContainer = destinationContainer;
            newContainer.Id = newContainer.ParentContainer.Containers.Count + 1;
            newContainer.ParentContainer.Containers.Add(newContainer);

            foreach (var element in xmlElement.ChildNodes)
            {
                if (!(element is XmlComment))
                {
                    foreach (var xContainer in sourceContainer.Containers)
                    {
                        var name = ((XmlElement)element).Name;
                        if (xContainer.Name == name)
                        {
                            FillChildNodes(newContainer, xContainer, (XmlElement)element);
                        }
                    }
                }
            }
        }