Exemple #1
0
        /// <summary>
        /// 写入
        /// </summary>
        public void Write(IMemento memento, Stream stream)
        {
            //1. 写入根结点内容
            XmlDocument    xmlDocument    = new XmlDocument();
            XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", "gb2312", "yes");

            xmlDocument.PrependChild(xmlDeclaration);

            XmlElement rootNode = xmlDocument.CreateElement(memento.Name);

            xmlDocument.AppendChild(rootNode);

            //2. 写入根结点属性信息
            for (int index = 0; index <= memento.GetAttributeCount() - 1; index++)
            {
                XmlAttribute attributeNode = xmlDocument.CreateAttribute(memento.GetAttributeName(index));
                attributeNode.Value = memento.GetAttributeValue(index);
                rootNode.SetAttributeNode(attributeNode);
            }

            //3. 写入子结点内容
            for (int index = 0; index <= memento.ChildCount - 1; index++)
            {
                IMemento childMemento = memento.GetChild(index);
                ProcessMemento(childMemento, rootNode, xmlDocument);
            }
            xmlDocument.Save(stream);
        }
Exemple #2
0
        private void ProcessMemento(IMemento memento, XmlNode parentNode, XmlDocument xmlDocument)
        {
            //1.增加结点
            XmlElement newNode = xmlDocument.CreateElement(memento.Name);

            parentNode.AppendChild(newNode);

            //2.设置属性信息
            for (int index = 0; index <= memento.GetAttributeCount() - 1; index++)
            {
                XmlAttribute attributeNode = xmlDocument.CreateAttribute(memento.GetAttributeName(index));
                attributeNode.Value = memento.GetAttributeValue(index);
                newNode.SetAttributeNode(attributeNode);
            }

            //3.递归进行ChildMemento处理
            for (int index = 0; index <= memento.ChildCount - 1; index++)
            {
                IMemento childMemento = memento.GetChild(index);
                ProcessMemento(childMemento, newNode, xmlDocument);
            }
        }