/// <summary> /// Adds an Element to the <see cref="XmlObject"/> but verifies it does not exist in the xml file first. /// </summary> /// <param name="elementname">The name of the element to create.</param> /// <param name="value">The value for the element.</param> /// <exception cref="Exception"> /// Thrown if the element already exists in the <see cref="XmlObject"/>. /// </exception> /// <exception cref="InvalidOperationException"> /// When called from a read-only instance. /// </exception> public void AddElement(string elementname, string value) { 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) { throw new Exception(Resources.XmlObject_Element_Already_Exists); } else { var xMLElementData = new XmlElementData { Attributes = null, Value = value, }; if (!this.ElementsAdded.ContainsKey(elementname)) { this.ElementsAdded.Add(elementname, xMLElementData); } else { this.ElementsAdded[elementname] = xMLElementData; } this.HasChanged = true; } }
// Summary: // Adds an Element to the XmlObject but verifies it does not exist first. // // Exceptions: // System.Exception: // Thrown if element already exists in the XMLObject. // System.ObjectDisposedException // XMLOblect is disposed. private void AddElement(string elementname, string value) { var elem = this.Doc.Root.Element(elementname); if (elem == null) { var xMLElementData = new XmlElementData { Attributes = null, Value = value, }; if (!this.ElementsAdded.ContainsKey(elementname)) { this.ElementsAdded.Add(elementname, xMLElementData); } else { this.ElementsAdded[elementname] = xMLElementData; } this.HasChanged = true; } else { throw new Exception("Element already exists."); } }
/// <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; } }
/// <summary> /// Writes an array of elements to the parrent element or updates them based /// upon the element name. /// /// If Elements do not exist yet they will be created automatically. /// </summary> /// <exception cref="ArgumentNullException">When <paramref name="values"/> is <see langword="null"/>.</exception> /// <exception cref="InvalidOperationException">When called from a read-only instance.</exception> /// <param name="parentelementname">parrent element name of the subelement.</param> /// <param name="elementname">The name to use when writing subelement(s).</param> /// <param name="values">The array of values to use for the subelement(s).</param> public void Write(string parentelementname, string elementname, string[] values) { if (values == null) { throw new ArgumentNullException(nameof(values)); } if (this.CachedXmlfilename.Equals(":memory", StringComparison.Ordinal)) { throw new InvalidOperationException(Resources.XmlObject_Instance_Read_Only); } else { var elem = this.Doc.Root.Element(parentelementname); if (elem == null) { this.Write(parentelementname, string.Empty); } var elem2 = this.Doc.Descendants(parentelementname); if (elem2.Any()) { // for Save() to work. this.Delete(parentelementname); } var xmleldata = new XmlElementData { Name = parentelementname, Subelements = new List <XmlElementData>(), }; foreach (var value in values) { var xmlelsubelement = new XmlElementData { Name = elementname, Value = value, }; xmleldata.Subelements.Add(xmlelsubelement); } if (this.ElementsAdded.ContainsKey(parentelementname)) { this.ElementsAdded[parentelementname] = xmleldata; } else { this.ElementsAdded.Add(parentelementname, xmleldata); } } }
/// <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."); } }
/// <summary> /// Writes to an element or updates it based upon the element name /// it is in and the value to place in it. /// /// If Element does not exist yet it will be created automatically. /// </summary> /// <exception cref="InvalidOperationException">When called from a read-only instance.</exception> /// <param name="elementname">The name of the element to write to or create.</param> /// <param name="value">The value for the element.</param> public void Write(string elementname, string value) { if (!this.CachedXmlfilename.Equals(":memory")) { var elem = this.Doc.Root.Element(elementname); if (elem != null || this.ElementsAdded.ContainsKey(elementname) || this.ElementsEdits.ContainsKey(elementname)) { var xMLElementData = new XmlElementData { Attributes = this.ElementsAdded.ContainsKey(elementname) ? this.ElementsAdded[elementname].Attributes : (this.ElementsEdits.ContainsKey(elementname) ? this.ElementsEdits[elementname].Attributes : null), Value = value, Name = elementname, }; if (this.ElementsAdded.ContainsKey(elementname)) { // modify this key, do not put into _elements_edits dictonary. this.ElementsAdded[elementname] = xMLElementData; } else { if (this.ElementsEdits.ContainsKey(elementname)) { // edit the collection whenever this changes. this.ElementsEdits[elementname] = xMLElementData; } else { this.ElementsEdits.Add(elementname, xMLElementData); } } this.HasChanged = true; } else { this.AddElement(elementname, value); } } else { throw new InvalidOperationException("This instance is read-only."); } }
/// <summary> /// Writes to an element or updates it based upon the element name /// it is in and the value to place in it. /// /// If Element does not exist yet it will be created automatically. /// </summary> /// <exception cref="InvalidOperationException">When called from a read-only instance.</exception> /// <param name="elementname">The name of the element to write to or create.</param> /// <param name="value">The value for the element.</param> public void Write(string elementname, string value) { 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.ElementsAdded.ContainsKey(elementname) || this.ElementsEdits.ContainsKey(elementname)) { var xMLElementData = new XmlElementData { Attributes = this.ElementsAdded.ContainsKey(elementname) ? this.ElementsAdded[elementname].Attributes : this.ElementsEdits.ContainsKey(elementname) ? this.ElementsEdits[elementname].Attributes : null, Value = value, Name = elementname, }; if (this.ElementsAdded.ContainsKey(elementname)) { // modify this key, do not put into _elements_edits dictonary. this.ElementsAdded[elementname] = xMLElementData; } else { if (this.ElementsEdits.ContainsKey(elementname)) { // edit the collection whenever this changes. this.ElementsEdits[elementname] = xMLElementData; } else { this.ElementsEdits.Add(elementname, xMLElementData); } } this.HasChanged = true; } else { this.AddElement(elementname, value); } }
/// <summary> /// Writes an array of elements to the parrent element or updates them based /// upon the element name. /// /// If Elements do not exist yet they will be created automatically. /// </summary> /// <exception cref="InvalidOperationException">When called from a read-only instance.</exception> /// <param name="parentelementname">parrent element name of the subelement.</param> /// <param name="elementname">The name to use when writing subelement(s).</param> /// <param name="values">The array of values to use for the subelement(s).</param> public void Write(string parentelementname, string elementname, string[] values) { if (!this.CachedXmlfilename.Equals(":memory")) { var elem = this.Doc.Root.Element(parentelementname); if (elem == null) { this.Write(parentelementname, string.Empty); } var elem2 = this.Doc.Descendants(parentelementname); if (elem2.Count() > 0) { // for Save() to work. this.Delete(parentelementname); } var xmleldata = new XmlElementData { Name = parentelementname, Subelements = new List <XmlElementData>(), }; foreach (var value in values) { var xmlelsubelement = new XmlElementData { Name = elementname, Value = value, }; xmleldata.Subelements.Add(xmlelsubelement); } if (this.ElementsAdded.ContainsKey(parentelementname)) { this.ElementsAdded[parentelementname] = xmleldata; } else { this.ElementsAdded.Add(parentelementname, xmleldata); } } else { throw new InvalidOperationException("This instance is read-only."); } }
// Summary: // Writes Added subelements to the XML file. private void SaveAddedSubelements(XElement xElement, XmlElementData elemdata) { if (!string.IsNullOrEmpty(elemdata.Name)) { var elem = new XElement(elemdata.Name, elemdata.Value); xElement.Add(elem); if (elemdata.Attributes != null) { foreach (var attributes in elemdata.Attributes) { elem.SetAttributeValue(attributes.AttributeName, attributes.Value); } } if (elemdata.Subelements != null) { // recursively add each subelement of these subelements. foreach (var element in elemdata.Subelements) { this.SaveAddedSubelements(elem, element); } } } }
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; }
/// <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."); } }