コード例 #1
0
        /// <summary>
        /// Creates and initializes an instance of the class by loading a serialized version of the instance from a file.
        /// </summary>
        /// <param name="filePath">Full path for the input file.</param>
        /// <returns>An instance of PFDirectoryEx.</returns>
        public static PFDirectoryEx LoadFromXmlFile(string filePath)
        {
            //*************************************************************
            //Built-in serialization does not work on PFDirectoryEx class.
            //*************************************************************
            //XmlSerializer deserializer = new XmlSerializer(typeof(PFDirectoryEx));
            //TextReader textReader = new StreamReader(filePath);
            //PFDirectoryEx dirEx;
            //dirEx = (PFDirectoryEx)deserializer.Deserialize(textReader);
            //textReader.Close();
            //return dirEx;

            PFDirectoryEx pfD‏irectoryExInstance = null;

            PFXmlDocument xmldoc = new PFXmlDocument();

            xmldoc.LoadFromFile(filePath);
            string     searchPath         = xmldoc.DocumentRootNodeName + "/" + "FullName";
            XmlElement fullNameNode       = xmldoc.FindElement(searchPath);
            string     PathForNewInstance = string.Empty;

            if (fullNameNode != null)
            {
                PathForNewInstance = fullNameNode.InnerText;
            }
            if (PathForNewInstance.Length > 0)
            {
                pfD‏irectoryExInstance = new PFDirectoryEx(PathForNewInstance, true);
            }

            return(pfD‏irectoryExInstance);
        }
コード例 #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);
        }