Exemplo n.º 1
0
        internal string AddToXML(InstructionSet newElement, string xPath, string elementName)
        {
            try {
                string path = GetApplicationPath() + "AppConfig.xml";

                var xmldoc = new XmlDocument();
                using (XmlReader xmlReader = XmlReader.Create(path))
                    xmldoc.Load(xmlReader);

                File.Copy(path, path + ".bak", true);
                File.Delete(path);

                XmlNode xmlnode = xmldoc.SelectSingleNode(xPath);          //"jaNET/Instructions");
                // InstructionSet
                XmlElement NewElement = xmldoc.CreateElement(elementName); //("InstructionSet");
                // ID
                XmlAttribute NewAttributeID = xmldoc.CreateAttribute("id");
                NewAttributeID.Value = newElement.Id;
                // Category
                XmlAttribute NewAttributeCateg = xmldoc.CreateAttribute("categ");
                NewAttributeCateg.Value = newElement.Category;
                // Header
                XmlAttribute NewAttributeHeader = xmldoc.CreateAttribute("header");
                NewAttributeHeader.Value = newElement.Header;
                // Short Description
                XmlAttribute NewAttributeShortDescr = xmldoc.CreateAttribute("shortdescr");
                NewAttributeShortDescr.Value = newElement.ShortDescription;
                // Description
                XmlAttribute NewAttributeDescr = xmldoc.CreateAttribute("descr");
                NewAttributeDescr.Value = newElement.Description;
                // Thumbnail
                XmlAttribute NewAttributeImg = xmldoc.CreateAttribute("img");
                NewAttributeImg.Value = newElement.ThumbnailUrl;
                // Reference to other Instruction Set
                XmlAttribute NewAttributeReference = xmldoc.CreateAttribute("ref");
                NewAttributeReference.Value = newElement.Reference;
                // Action
                NewElement.InnerText = newElement.Action;

                if (NewAttributeID.Value != string.Empty)
                {
                    NewElement.SetAttributeNode(NewAttributeID);
                }
                if (NewAttributeCateg.Value != string.Empty)
                {
                    NewElement.SetAttributeNode(NewAttributeCateg);
                }
                if (NewAttributeHeader.Value != string.Empty)
                {
                    NewElement.SetAttributeNode(NewAttributeHeader);
                }
                if (NewAttributeShortDescr.Value != string.Empty)
                {
                    NewElement.SetAttributeNode(NewAttributeShortDescr);
                }
                if (NewAttributeDescr.Value != string.Empty)
                {
                    NewElement.SetAttributeNode(NewAttributeDescr);
                }
                if (NewAttributeImg.Value != string.Empty)
                {
                    NewElement.SetAttributeNode(NewAttributeImg);
                }
                if (NewAttributeReference.Value != string.Empty)
                {
                    NewElement.SetAttributeNode(NewAttributeReference);
                }

                if (NewAttributeID.Value == string.Empty)
                {
                    xmlnode.SelectSingleNode(elementName).InnerText = NewElement.InnerText;
                }
                else
                {
                    xmlnode.InsertAfter(NewElement, xmlnode.LastChild);
                    xmldoc.DocumentElement.AppendChild(xmlnode);
                }

                xmldoc.Save(path);

                return("Element added.");
            }
            catch (Exception e) {
                return(string.Format("{0}\r\nPlease try again.", e.Message));
            }
        }
Exemplo n.º 2
0
        internal string AddToXML(InstructionSet newElement, string xPath, string elementName)
        {
            try
            {
                string path = GetApplicationPath() + "AppConfig.xml";

                var xmldoc = new XmlDocument();
                using (XmlReader xmlReader = XmlReader.Create(path))
                    xmldoc.Load(xmlReader);

                File.Copy(path, path + ".bak", true);
                File.Delete(path);

                XmlNode xmlnode = xmldoc.SelectSingleNode(xPath); //"jaNET/Instructions");
                // InstructionSet
                XmlElement NewElement = xmldoc.CreateElement(elementName); //("InstructionSet");
                // ID
                XmlAttribute NewAttributeID = xmldoc.CreateAttribute("id");
                NewAttributeID.Value = newElement.Id;
                // Category
                XmlAttribute NewAttributeCateg = xmldoc.CreateAttribute("categ");
                NewAttributeCateg.Value = newElement.Category;
                // Header
                XmlAttribute NewAttributeHeader = xmldoc.CreateAttribute("header");
                NewAttributeHeader.Value = newElement.Header;
                // Short Description
                XmlAttribute NewAttributeShortDescr = xmldoc.CreateAttribute("shortdescr");
                NewAttributeShortDescr.Value = newElement.ShortDescription;
                // Description
                XmlAttribute NewAttributeDescr = xmldoc.CreateAttribute("descr");
                NewAttributeDescr.Value = newElement.Description;
                // Thumbnail
                XmlAttribute NewAttributeImg = xmldoc.CreateAttribute("img");
                NewAttributeImg.Value = newElement.ThumbnailUrl;
                // Reference to other Instruction Set
                XmlAttribute NewAttributeReference = xmldoc.CreateAttribute("ref");
                NewAttributeReference.Value = newElement.Reference;
                // Action
                NewElement.InnerText = newElement.Action;

                if (NewAttributeID.Value != string.Empty)
                    NewElement.SetAttributeNode(NewAttributeID);
                if (NewAttributeCateg.Value != string.Empty)
                    NewElement.SetAttributeNode(NewAttributeCateg);
                if (NewAttributeHeader.Value != string.Empty)
                    NewElement.SetAttributeNode(NewAttributeHeader);
                if (NewAttributeShortDescr.Value != string.Empty)
                    NewElement.SetAttributeNode(NewAttributeShortDescr);
                if (NewAttributeDescr.Value != string.Empty)
                    NewElement.SetAttributeNode(NewAttributeDescr);
                if (NewAttributeImg.Value != string.Empty)
                    NewElement.SetAttributeNode(NewAttributeImg);
                if (NewAttributeReference.Value != string.Empty)
                    NewElement.SetAttributeNode(NewAttributeReference);

                if (NewAttributeID.Value == string.Empty)
                    xmlnode.SelectSingleNode(elementName).InnerText = NewElement.InnerText;
                else
                {
                    xmlnode.InsertAfter(NewElement, xmlnode.LastChild);
                    xmldoc.DocumentElement.AppendChild(xmlnode);
                }

                xmldoc.Save(path);

                return "Element added.";
            }
            catch (Exception e)
            {
                return string.Format("{0}\r\nPlease try again.", e.Message);
            }
        }