SerializeToXmlElement() protected method

protected SerializeToXmlElement ( XmlWriter writer, String elementName ) : bool
writer System.Xml.XmlWriter
elementName String
return bool
        protected internal virtual string SerializeSection(ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode)
        {
            if (((base.CurrentConfiguration != null) && (base.CurrentConfiguration.TargetFramework != null)) && !this.ShouldSerializeSectionInTargetVersion(base.CurrentConfiguration.TargetFramework))
            {
                return(string.Empty);
            }
            ConfigurationElement.ValidateElement(this, null, true);
            ConfigurationElement element = ConfigurationElement.CreateElement(base.GetType());

            element.Unmerge(this, parentElement, saveMode);
            StringWriter  w      = new StringWriter(CultureInfo.InvariantCulture);
            XmlTextWriter writer = new XmlTextWriter(w)
            {
                Formatting  = Formatting.Indented,
                Indentation = 4,
                IndentChar  = ' '
            };

            element.DataToWriteInternal = saveMode != ConfigurationSaveMode.Minimal;
            if ((base.CurrentConfiguration != null) && (base.CurrentConfiguration.TargetFramework != null))
            {
                base._configRecord.SectionsStack.Push(this);
            }
            element.SerializeToXmlElement(writer, name);
            if ((base.CurrentConfiguration != null) && (base.CurrentConfiguration.TargetFramework != null))
            {
                base._configRecord.SectionsStack.Pop();
            }
            writer.Flush();
            return(w.ToString());
        }
        protected internal override bool SerializeElement(XmlWriter writer, bool serializeCollectionKey)
        {
            if (serializeCollectionKey)
            {
                return(base.SerializeElement(writer, serializeCollectionKey));
            }

            bool wroteData = false;

            if (IsBasic)
            {
                for (int n = 0; n < list.Count; n++)
                {
                    ConfigurationElement elem = (ConfigurationElement)list [n];
                    if (ElementName != string.Empty)
                    {
                        wroteData = elem.SerializeToXmlElement(writer, ElementName) || wroteData;
                    }
                    else
                    {
                        wroteData = elem.SerializeElement(writer, false) || wroteData;
                    }
                }
            }
            else
            {
                if (emitClear)
                {
                    writer.WriteElementString(clearElementName, "");
                    wroteData = true;
                }

                if (removed != null)
                {
                    for (int n = 0; n < removed.Count; n++)
                    {
                        writer.WriteStartElement(removeElementName);
                        ((ConfigurationElement)removed[n]).SerializeElement(writer, true);
                        writer.WriteEndElement();
                    }
                    wroteData = wroteData || removed.Count > 0;
                }

                for (int n = 0; n < list.Count; n++)
                {
                    ConfigurationElement elem = (ConfigurationElement)list [n];
                    elem.SerializeToXmlElement(writer, addElementName);
                }

                wroteData = wroteData || list.Count > 0;
            }
            return(wroteData);
        }
Beispiel #3
0
        protected internal virtual bool SerializeElement(XmlWriter writer, bool serializeCollectionKey)
        {
            PreSerialize(writer);

            if (serializeCollectionKey)
            {
                ConfigurationPropertyCollection props = GetKeyProperties();
                foreach (ConfigurationProperty prop in props)
                {
                    writer.WriteAttributeString(prop.Name, prop.ConvertToString(this[prop.Name]));
                }
                return(props.Count > 0);
            }

            bool wroteData = false;

            foreach (PropertyInformation prop in ElementInformation.Properties)
            {
                if (prop.IsElement)
                {
                    continue;
                }

                if (saveContext == null)
                {
                    throw new InvalidOperationException();
                }
                if (!saveContext.HasValue(prop))
                {
                    continue;
                }

                writer.WriteAttributeString(prop.Name, prop.GetStringValue());
                wroteData = true;
            }

            foreach (PropertyInformation prop in ElementInformation.Properties)
            {
                if (!prop.IsElement)
                {
                    continue;
                }

                ConfigurationElement val = (ConfigurationElement)prop.Value;
                if (val != null)
                {
                    wroteData = val.SerializeToXmlElement(writer, prop.Name) || wroteData;
                }
            }
            return(wroteData);
        }
Beispiel #4
0
        protected internal virtual bool SerializeElement(XmlWriter writer, bool serializeCollectionKey)
        {
            PreSerialize(writer);

            if (serializeCollectionKey)
            {
                ConfigurationPropertyCollection props = GetKeyProperties();
                foreach (ConfigurationProperty prop in props)
                {
                    writer.WriteAttributeString(prop.Name, prop.ConvertToString(this[prop.Name]));
                }
                return(props.Count > 0);
            }

            bool wroteData = false;

            foreach (PropertyInformation prop in ElementInformation.Properties)
            {
                if (prop.IsElement || prop.ValueOrigin == PropertyValueOrigin.Default)
                {
                    continue;
                }

                if (!object.Equals(prop.Value, prop.DefaultValue))
                {
                    writer.WriteAttributeString(prop.Name, prop.GetStringValue());
                    wroteData = true;
                }
            }

            foreach (PropertyInformation prop in ElementInformation.Properties)
            {
                if (!prop.IsElement)
                {
                    continue;
                }

                ConfigurationElement val = (ConfigurationElement)prop.Value;
                if (val != null)
                {
                    wroteData = val.SerializeToXmlElement(writer, prop.Name) || wroteData;
                }
            }
            return(wroteData);
        }
Beispiel #5
0
        protected internal virtual string SerializeSection(ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode)
        {
            ValidateElement(this, null, true);

            ConfigurationElement TempElement = CreateElement(this.GetType());

            TempElement.Unmerge(this, parentElement, saveMode);

            StringWriter  strWriter = new StringWriter(CultureInfo.InvariantCulture);
            XmlTextWriter writer    = new XmlTextWriter(strWriter);

            writer.Formatting  = Formatting.Indented;
            writer.Indentation = 4;
            writer.IndentChar  = ' ';
            TempElement.DataToWriteInternal = (saveMode != ConfigurationSaveMode.Minimal);
            TempElement.SerializeToXmlElement(writer, name);
            writer.Flush();
            return(strWriter.ToString());
        }
        protected internal virtual string SerializeSection(ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode)
        {
            if (CurrentConfiguration != null &&
                CurrentConfiguration.TargetFramework != null &&
                !ShouldSerializeSectionInTargetVersion(CurrentConfiguration.TargetFramework))
            {
                return(string.Empty);
            }

            ValidateElement(this, null, true);

            ConfigurationElement TempElement = CreateElement(this.GetType());

            TempElement.Unmerge(this, parentElement, saveMode);

            StringWriter  strWriter = new StringWriter(CultureInfo.InvariantCulture);
            XmlTextWriter writer    = new XmlTextWriter(strWriter);

            writer.Formatting  = Formatting.Indented;
            writer.Indentation = 4;
            writer.IndentChar  = ' ';
            TempElement.DataToWriteInternal = (saveMode != ConfigurationSaveMode.Minimal);

            if (CurrentConfiguration != null && CurrentConfiguration.TargetFramework != null)
            {
                _configRecord.SectionsStack.Push(this);
            }

            TempElement.SerializeToXmlElement(writer, name);

            if (CurrentConfiguration != null && CurrentConfiguration.TargetFramework != null)
            {
                _configRecord.SectionsStack.Pop();
            }

            writer.Flush();
            return(strWriter.ToString());
        }