Пример #1
0
        public void WriteXml(XmlWriter w)
        {
            w.WriteStartDocument();
            w.WriteStartElement("Settings");
            ArrayList keys = new ArrayList(map.Keys);

            keys.Sort();
            foreach (string key in keys)
            {
                object value = map[key];
                if (value == null)
                {
                    continue;
                }

                PropertyInfo pi = this.GetType().GetProperty(key);
                if (pi != null)
                {
                    Type t = pi.PropertyType;
                    if (t == typeof(Point))
                    {
                        w.WriteStartElement(key);
                        SerializePoint(w, (Point)value);
                        w.WriteEndElement();
                    }
                    else if (t == typeof(Size))
                    {
                        SerializeSize(w, key, (Size)value);
                    }
                    else if (t == typeof(int))
                    {
                        w.WriteElementString(key, ((int)value).ToString());
                    }
                    else if (t == typeof(string))
                    {
                        w.WriteElementString(key, ((string)value));
                    }
                    else if (t == typeof(GraphState))
                    {
                        w.WriteStartElement(key);
                        this.GraphState.WriteXml(w);
                        w.WriteEndElement();
                    }
                    else if (t == typeof(DateTime))
                    {
                        SerializeDateTime(w, key, (DateTime)value);
                    }
                    else if (t == typeof(TimeSpan))
                    {
                        SerializeTimeSpan(w, key, (TimeSpan)value);
                    }
                    else if (t == typeof(bool))
                    {
                        w.WriteElementString(key, ((bool)value).ToString());
                    }
                    else if (t == typeof(QueryRow[]))
                    {
                        w.WriteStartElement(key);
                        WriteQuery(w, (QueryRow[])value);
                        w.WriteEndElement();
                    }
                    else
                    {
                        throw new Exception("Settings.ReadXml encountered unsupported property type '" + t.FullName + "'");
                    }
                }
                else if (value is IXmlSerializable)
                {
                    IXmlSerializable s = (IXmlSerializable)value;
                    if (s != null)
                    {
                        w.WriteStartElement(key);
                        s.WriteXml(w);
                        w.WriteEndElement();
                    }
                }
                else
                {
                    w.WriteElementString(key, value.ToString());
                }
            }

            HashSet <Type> saved = new HashSet <Type>();

            foreach (KeyValuePair <Type, ViewState> pair in this.viewStates)
            {
                Type      t = pair.Key;
                ViewState s = pair.Value;
                if (s != null)
                {
                    saved.Add(t);
                    string key = s.GetType().FullName;
                    w.WriteStartElement(key);
                    w.WriteAttributeString("ViewType", t.FullName);
                    s.WriteXml(w);
                    w.WriteEndElement();
                }
            }

            foreach (KeyValuePair <Type, XmlElement> pair in this.viewStateNodes)
            {
                Type t = pair.Key;
                if (!saved.Contains(t))
                {
                    // this view type was not used, but we need to round trip the previous settings so we don't lose them.
                    XmlElement e = pair.Value;
                    if (e != null)
                    {
                        e.WriteTo(w);
                    }
                }
            }

            w.WriteEndElement();
            w.WriteEndDocument();
        }