Пример #1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="instance"></param>
        /// <returns></returns>
        public string Serialize(object instance)
        {
            if (instance == null)
                throw new ArgumentNullException("instance");
            var writer = new XmlWriter { Owner = this };

            var type = instance.GetType();
            var doc = new XDocument();
            XElement root = null;
            if (Types.ByteArray == type)
                root = writer.WriteByteArray(instance as byte[]);
            else if (type.IsCollectionTypeExcludeStringAndDictionaryType())
                root = writer.WriteList(instance as IEnumerable);
            else //TODO:字典,泛型对象需要检查 
            {
                var name = type.Name;
                root = new XElement(name.AsNamespaced(Namespace));
                writer.Write(root, instance);
            }

            if (RootElement.HasValue())
                doc.Add(new XElement(RootElement.AsNamespaced(Namespace), root));
            else
                doc.Add(root);

            return doc.ToString();
        }