예제 #1
0
        public void Serialize(XmlTextWriter writer, object obj, bool writeElement)
        {
            //Use the IXmlSerializable implementation if needed
            IXmlSerializable serializableObject = obj as IXmlSerializable;

            if (serializableObject != null)
            {
                serializableObject.WriteXml(writer);
                return;
            }

            //If the type can be converted directly to a string then serialize it
            //as an element with text content (int, string, etc)
            TypeConverter conv = TypeDescriptor.GetConverter(obj);

            if (CanSerialize(conv))
            {
                writer.WriteElementString(obj.GetType().Name.ToLowerInvariant(), conv.ConvertToInvariantString(obj));
                return;
            }

            //Serialize the object and its public members
            if (writeElement)
            {
                writer.WriteStartElement(obj.GetType().Name);
            }

            //Get the list of properties with the ones that want to be serialized as attributed listed first
            List <PropertyDescriptor> props = GetSortedProperties(obj);

            //Serialize the Properties
            foreach (PropertyDescriptor prop in props)
            {
                //Get the property value
                object value = prop.GetValue(obj);
                if (value == null)
                {
                    continue;
                }

                if (prop.IsReadOnly)
                {
                    //if the property is read-only and is a list then serialize the content
                    IList list = value as IList;
                    if (list != null && list.Count > 0)
                    {
                        writer.WriteStartElement(prop.Name);

                        string elementName = prop.Name.Substring(0, prop.Name.Length - 1);
                        DefaultListSerializer serializer = new DefaultListSerializer(elementName);
                        serializer.Serialize(writer, list, false);

                        writer.WriteEndElement();
                    }
                }
                else
                {
                    //Get a converter and Serialize it to a string
                    TypeConverter converter = prop.Converter;
                    if (converter == null || !converter.CanConvertTo(typeof(string)) || !converter.CanConvertFrom(typeof(string)))
                    {
                        writer.WriteStartElement(prop.Name);
                        ObjectSerializer.Serialize(writer, value, false);
                        writer.WriteEndElement();
                        continue;
                    }

                    string strValue = converter.ConvertToString(null, InvariantSerializationCulture, value);

                    if (prop.Attributes.Contains(new XmlAttributeAttribute()))
                    {
                        writer.WriteAttributeString(prop.Name, strValue);
                    }
                    else
                    {
                        writer.WriteElementString(prop.Name, strValue);
                    }
                }
            }

            if (writeElement)
            {
                writer.WriteEndElement(); //Object element
            }
        }