/// <summary> /// Saves the property bag to a new XML document. /// </summary> /// <param name="topElementName"></param> /// <returns></returns> public void Save(XmlElement element, XmlNamespaceManagerEx manager, SimpleXmlSaveMode mode) { if (element == null) { throw new ArgumentNullException("element"); } if (manager == null) { throw new ArgumentNullException("manager"); } // raise... this.OnSaving(); // clear? if (mode == SimpleXmlSaveMode.ClearAllChildren) { while (element.FirstChild != null) { element.RemoveChild(element.FirstChild); } } // mbr - 20-10-2005 - to do this, we want to sort the names so that we are always serializing in a consistent order... ArrayList names = new ArrayList(this.InnerValues.Keys); names.Sort(); // walk and add items... foreach (string name in names) { // find a child with the name... object value = this.InnerValues[name]; try { if ((mode & SimpleXmlSaveMode.UseAttributes) != 0) { this.SaveValueAsAttribute(element, name, value); } else { this.SaveValueAsElement(element, name, value, mode); } } catch (Exception ex) { throw new InvalidOperationException(string.Format("Failed to append value '{0}'. ({1})", name, value), ex); } } // raise... this.OnSaved(); }
private void SaveValueAsElement(XmlElement element, string name, object value, SimpleXmlSaveMode mode) { if (element == null) { throw new ArgumentNullException("element"); } if (name == null) { throw new ArgumentNullException("name"); } if (name.Length == 0) { throw new ArgumentOutOfRangeException("'name' is zero-length."); } XmlElement child = null; // load the existing one if we're merging... if (mode == SimpleXmlSaveMode.ReplaceExisting) { child = (XmlElement)element.SelectSingleNode(name); } // create one if we need one... if (child == null) { child = element.OwnerDocument.CreateElement(name); element.AppendChild(child); } // set... if (!(value is MissingItem)) { // mbr - 10-05-2006 - added null check. if (value == null || !(value.GetType().IsArray)) { XmlHelper.SetElementValue(child, value); } else { XmlHelper.SetAttributeValue(child, isArrayAttributeName, true); // We have an array so we will walk the array foreach (object elementValue in (Array)value) { XmlElement childElementValue = element.OwnerDocument.CreateElement("value"); child.AppendChild(childElementValue); XmlHelper.SetElementValue(childElementValue, elementValue); } } } }
public void Save(XmlElement element, SimpleXmlSaveMode mode) { this.Save(element, XmlHelper.GetNamespaceManagerEx(element.OwnerDocument), mode); }