コード例 #1
0
ファイル: Tests.cs プロジェクト: mcondon013/pfcodelibrary
        public static void RunTest_Memory(string _filename, string _rootNode)
        {
            _xmlDoc = new PFXmlDocument(_rootNode);

            XmlElement xmlElement;
            XmlElement saveXmlElement;

            xmlElement     = _xmlDoc.AddNewElement(_xmlDoc.DocumentRootNode, "ProcessingDate", DateTime.Now.ToString());
            saveXmlElement = _xmlDoc.AddNewElement(_xmlDoc.DocumentRootNode, "IsEnabled", "True");
            xmlElement     = _xmlDoc.AddNewElement(saveXmlElement, "node1", "text1");
            xmlElement     = _xmlDoc.AddNewElement(saveXmlElement, "node2", "text2");
            _xmlDoc.AddNewAttribute(xmlElement, "attr1", "val1");
            xmlElement = _xmlDoc.AddNewElement(saveXmlElement, "node3", "text3");
            _xmlDoc.AddNewAttribute(xmlElement, "attr3a", "val3a");
            _xmlDoc.AddNewAttribute(xmlElement, "attr3b", "val3b");
            xmlElement = _xmlDoc.AddNewElement(_xmlDoc.DocumentRootNode, "FormBackground", "Red");
            _xmlDoc.SaveToFile(_filename);

            PFXmlDocument xmlDoc2 = new PFXmlDocument();

            xmlDoc2.LoadFromFile(_filename);
            Program._messageLog.WriteLine(xmlDoc2.OuterXml);
            Program._messageLog.WriteLine("\r\n" + xmlDoc2.InnerText);
            Program._messageLog.WriteLine("\r\n" + xmlDoc2.InnerXml);
        }
コード例 #2
0
ファイル: Tests.cs プロジェクト: mcondon013/pfcodelibrary
        public static void AddNodes(MainForm frm)
        {
            bool          err = false;
            XmlElement    xmlElement;
            StringBuilder nodePath = new StringBuilder();

            Program._messageLog.Clear();

            if (frm.txtNodePath.Text.Length == 0)
            {
                Program._messageLog.WriteLine("Node path must be specified.");
                err = true;
            }
            if (err)
            {
                return;
            }

            if (frm.txtNodePath.Text.Length > 0)
            {
                if (frm.txtNodeTag.Text.Length > 0)
                {
                    nodePath.Length = 0;
                    nodePath.Append(frm.txtRootNode.Text);
                    nodePath.Append("/");
                    nodePath.Append(frm.txtNodePath.Text);
                    try
                    {
                        xmlElement = _xmlDoc.FindElement(nodePath.ToString());
                        if (xmlElement == null)
                        {
                            xmlElement = _xmlDoc.FindElement(frm.txtRootNode.Text);
                            _xmlDoc.AddNewElement(xmlElement, frm.txtNodePath.Text, string.Empty);
                            xmlElement = _xmlDoc.FindElement(nodePath.ToString());
                        }
                        _xmlDoc.AddNewElement((XmlNode)xmlElement, frm.txtNodeTag.Text, frm.txtNodeInnerText.Text);
                    }
                    catch (Exception ex)
                    {
                        _msg.Length = 0;
                        _msg.Append("Attempt to add element failed.\n");
                        _msg.Append("Node tag: ");
                        _msg.Append(frm.txtNodeTag.Text);
                        _msg.Append("\n\n");
                        _msg.Append(AppMessages.FormatErrorMessage(ex));
                        AppMessages.DisplayErrorMessage(_msg.ToString());
                    }
                    _xmlDoc.SaveToFile(frm.txtFilename.Text);
                }

                if (frm.txtAttribute.Text.Length > 0)
                {
                    nodePath.Length = 0;
                    nodePath.Append(frm.txtRootNode.Text);
                    nodePath.Append("/");
                    nodePath.Append(frm.txtNodePath.Text);
                    if (frm.txtNodeTag.Text.Length > 0)
                    {
                        nodePath.Append("/");
                        nodePath.Append(frm.txtNodeTag.Text);
                    }
                    try
                    {
                        xmlElement = _xmlDoc.FindElement(nodePath.ToString());
                        _xmlDoc.AddNewAttribute(xmlElement, frm.txtAttribute.Text, frm.txtAttributeValue.Text);
                    }
                    catch (Exception ex)
                    {
                        _msg.Length = 0;
                        _msg.Append("Attempt to add attribute failed.\n");
                        _msg.Append("Node tag: ");
                        _msg.Append(frm.txtNodeTag.Text);
                        _msg.Append("\n\n");
                        _msg.Append(AppMessages.FormatErrorMessage(ex));
                        AppMessages.DisplayErrorMessage(_msg.ToString());
                    }
                    _xmlDoc.SaveToFile(frm.txtFilename.Text);
                }
            }

            OutputContentsOfFile(frm.txtFilename.Text);
        }