예제 #1
0
파일: XmlObject.cs 프로젝트: fadinglr/Sdk
        /// <summary>
        /// Removes an xml attribute using the element name and the name of the attribute.
        /// </summary>
        /// <exception cref="ArgumentException">elementname or attributename does not exist in the xml, in pending edits, or was already deleted.</exception>
        /// <exception cref="InvalidOperationException">When the object is a read-only instance.</exception>
        /// <param name="elementname">The element name that has the attribute to delete.</param>
        /// <param name="attributename">The name of the attribute to delete.</param>
        public void Delete(string elementname, string attributename)
        {
            if (this.CachedXmlfilename.Equals(":memory", StringComparison.Ordinal))
            {
                throw new InvalidOperationException(Resources.XmlObject_Instance_Read_Only);
            }
            else
            {
                var elem = this.Doc.Root.Element(elementname);
                if (this.ElementsAdded.ContainsKey(elementname))
                {
                    foreach (var attribute in this.ElementsAdded[elementname].Attributes)
                    {
                        if (attribute.AttributeName.Equals(attributename, StringComparison.Ordinal))
                        {
                            _ = this.ElementsAdded[elementname].Attributes.Remove(attribute);
                        }
                    }
                }
                else if (this.ElementsEdits.ContainsKey(elementname))
                {
                    foreach (var attribute in this.ElementsEdits[elementname].Attributes)
                    {
                        if (attribute.AttributeName.Equals(attributename, StringComparison.Ordinal))
                        {
                            _ = this.ElementsEdits[elementname].Attributes.Remove(attribute);
                        }
                    }
                }
                else if (elem != null && elem.Attribute(attributename) != null &&
                         !this.ElementAttributesDeleted.ContainsKey(elementname))
                {
                    var xmleldata = new XmlElementData
                    {
                        Name       = elementname,
                        Attributes = new List <XmlAttributeData>(),
                    };
                    var xMLAttributeData = new XmlAttributeData
                    {
                        AttributeName = attributename,
                        Value         = null,
                    };
                    xmleldata.Attributes.Add(xMLAttributeData);
                    this.ElementAttributesDeleted.Add(elementname, xmleldata);
                }
                else
                {
                    throw new ArgumentException(Resources.XmlObject_Element_Or_Attribute_Already_Deleted);
                }

                this.HasChanged = true;
            }
        }
예제 #2
0
 /// <summary>
 /// Removes an xml attribute using the element name and the name of the attribute.
 /// </summary>
 /// <exception cref="ArgumentException">elementname or attributename does not exist in the xml or in pending edits.</exception>
 /// <exception cref="InvalidOperationException">When the object is a read-only instance.</exception>
 /// <param name="elementname">The element name that has the attribute to delete.</param>
 /// <param name="attributename">The name of the attribute to delete.</param>
 public void Delete(string elementname, string attributename)
 {
     if (!this.CachedXmlfilename.Equals(":memory"))
     {
         var elem = this.Doc.Root.Element(elementname);
         if (this.ElementsAdded.ContainsKey(elementname))
         {
             foreach (var attribute in this.ElementsAdded[elementname].Attributes)
             {
                 if (attribute.AttributeName.Equals(attributename))
                 {
                     this.ElementsAdded[elementname].Attributes.Remove(attribute);
                 }
             }
         }
         else if (this.ElementsEdits.ContainsKey(elementname))
         {
             foreach (var attribute in this.ElementsEdits[elementname].Attributes)
             {
                 if (attribute.AttributeName.Equals(attributename))
                 {
                     this.ElementsEdits[elementname].Attributes.Remove(attribute);
                 }
             }
         }
         else if (elem != null && elem.Attribute(attributename) != null)
         {
             var xmleldata = new XmlElementData
             {
                 Name       = elementname,
                 Attributes = new List <XmlAttributeData>(),
             };
             var xMLAttributeData = new XmlAttributeData
             {
                 AttributeName = attributename,
                 Value         = null,
             };
             xmleldata.Attributes.Add(xMLAttributeData);
             this.ElementAttributesDeleted.Add(elementname, xmleldata);
         }
         else
         {
             throw new ArgumentException("elementname or attributename does not exist in the xml or in pending edits.");
         }
     }
     else
     {
         throw new InvalidOperationException("This instance is read-only.");
     }
 }
예제 #3
0
파일: XmlObject.cs 프로젝트: fadinglr/Sdk
        public void AddAttribute(string elementname, string attributename, object attributevalue)
        {
            if (this.CachedXmlfilename.Equals(":memory", StringComparison.Ordinal))
            {
                throw new InvalidOperationException(Resources.XmlObject_Instance_Read_Only);
            }

            var elem = this.Doc.Root.Element(elementname);

            if (elem == null)
            {
                this.Write(elementname, string.Empty);
            }

            if (this.ElementsAdded.ContainsKey(elementname))
            {
                var xmleldata = this.ElementsAdded[elementname];
                if (xmleldata.Attributes != null)
                {
                    foreach (var attribute in xmleldata.Attributes)
                    {
                        if (attribute.AttributeName.Equals(attributename, StringComparison.Ordinal))
                        {
                            attribute.Value = attributevalue.ToString();
                        }
                    }
                }

                var xMLAttributeData = new XmlAttributeData
                {
                    AttributeName = attributename,
                    Value         = attributevalue.ToString(),
                };
                xmleldata.Attributes ??= new List <XmlAttributeData>();
                xmleldata.Attributes.Add(xMLAttributeData);
            }
            else if (this.ElementsEdits.ContainsKey(elementname))
            {
                XmlAttributeData xMLAttributeData;
                var edit           = false;
                var attributeIndex = 0;
                var xmleldata      = this.ElementsEdits[elementname];
                foreach (var attribute in xmleldata.Attributes)
                {
                    if (attribute.AttributeName.Equals(attributename, StringComparison.Ordinal))
                    {
                        edit           = true;
                        attributeIndex = xmleldata.Attributes.IndexOf(attribute);
                    }
                }

                xMLAttributeData = new XmlAttributeData
                {
                    AttributeName = attributename,
                    Value         = attributevalue.ToString(),
                };
                xmleldata.Attributes ??= new List <XmlAttributeData>();
                if (!edit && attributevalue != null)
                {
                    xmleldata.Attributes.Add(xMLAttributeData);
                }
                else
                {
                    xMLAttributeData       = xmleldata.Attributes[attributeIndex];
                    xMLAttributeData.Value = attributevalue.ToString();
                }
            }
            else
            {
                if (attributevalue != null)
                {
                    if (elem.Attribute(attributename) != null)
                    {
                        throw new Exception(Resources.XmlObject_Attribute_Already_Exists);
                    }
                    else
                    {
                        var xmleldata = new XmlElementData
                        {
                            Name = elementname,
                        };
                        var xMLAttributeData = new XmlAttributeData
                        {
                            AttributeName = attributename,
                            Value         = attributevalue.ToString(),
                        };
                        xmleldata.Attributes = new List <XmlAttributeData>
                        {
                            xMLAttributeData,
                        };
                        this.ElementsEdits.Add(elementname, xmleldata);
                    }
                }
            }

            this.HasChanged = true;
        }
예제 #4
0
        /// <summary>
        /// Adds or edits an attribute in an Element and sets it's value in the <see cref="XmlObject"/>.
        ///
        /// This method can also remove the attribute by setting the value to null.
        ///
        /// If Element does not exist yet it will be created automatically with an
        /// empty value as well as making the attribute as if the Element was
        /// pre-added before calling this function.
        /// </summary>
        /// <exception cref="Exception">Attribute already exists in the xml file.</exception>
        /// <exception cref="InvalidOperationException">When called from a read-only instance.</exception>
        /// <param name="elementname">The name of the element to add a attribute to.</param>
        /// <param name="attributename">The name of the attribute to add.</param>
        /// <param name="attributevalue">The value of the attribute.</param>
        public void AddAttribute(string elementname, string attributename, object attributevalue)
        {
            if (!this.CachedXmlfilename.Equals(":memory"))
            {
                var elem = this.Doc.Root.Element(elementname);
                if (elem == null)
                {
                    this.Write(elementname, string.Empty);
                }

                if (this.ElementsAdded.ContainsKey(elementname))
                {
                    var xmleldata = this.ElementsAdded[elementname];
                    var found     = false;
                    if (xmleldata.Attributes != null)
                    {
                        foreach (var attribute in xmleldata.Attributes)
                        {
                            if (attribute.AttributeName.Equals(attributename))
                            {
                                found = true;
                                attribute.AttributeName = attributevalue.ToString();
                            }
                        }
                    }

                    if (found)
                    {
                        var xMLAttributeData = new XmlAttributeData
                        {
                            AttributeName = attributename,
                            Value         = attributevalue.ToString(),
                        };
                        xmleldata.Attributes = xmleldata.Attributes ?? new List <XmlAttributeData>();
                        xmleldata.Attributes.Add(xMLAttributeData);
                    }
                }
                else if (this.ElementsEdits.ContainsKey(elementname))
                {
                    XmlAttributeData xMLAttributeData;
                    var edit           = false;
                    var attributeIndex = 0;
                    var xmleldata      = this.ElementsEdits[elementname];
                    foreach (var attribute in xmleldata.Attributes)
                    {
                        if (attribute.AttributeName.Equals(attributename))
                        {
                            edit           = true;
                            attributeIndex = xmleldata.Attributes.IndexOf(attribute);
                        }
                    }

                    xMLAttributeData = new XmlAttributeData
                    {
                        AttributeName = attributename,
                        Value         = attributevalue.ToString(),
                    };
                    xmleldata.Attributes = xmleldata.Attributes ?? new List <XmlAttributeData>();
                    if (!edit && attributevalue != null)
                    {
                        xmleldata.Attributes.Add(xMLAttributeData);
                    }
                    else
                    {
                        xMLAttributeData       = xmleldata.Attributes[attributeIndex];
                        xMLAttributeData.Value = attributevalue.ToString();
                    }
                }
                else
                {
                    if (attributevalue != null)
                    {
                        if (elem.Attribute(attributename) != null)
                        {
                            throw new Exception("Attribute already exists.");
                        }
                        else
                        {
                            var xmleldata = new XmlElementData
                            {
                                Name = elementname,
                            };
                            var xMLAttributeData = new XmlAttributeData
                            {
                                AttributeName = attributename,
                                Value         = attributevalue.ToString(),
                            };
                            xmleldata.Attributes = new List <XmlAttributeData>
                            {
                                xMLAttributeData,
                            };
                            this.ElementsEdits.Add(elementname, xmleldata);
                        }
                    }
                }

                this.HasChanged = true;
            }
            else
            {
                throw new InvalidOperationException("This instance is read-only.");
            }
        }