Ejemplo n.º 1
0
        public XmlWriter ObjectToXmlNode(object o, XmlWriter writer, PropertyInfo property)
        {
            var    dic          = o as IDictionary;
            string propertyName = property.Name;
            var    attr         = property.GetCustomAttributes(typeof(XmlAttributeAttribute), true)?.FirstOrDefault();

            if (attr != null)
            {
                propertyName = (attr as XmlAttributeAttribute).AttributeName;
            }
            else
            {
                var arrayItemAttr = property.GetCustomAttributes(typeof(XmlArrayItemAttribute), true)?.FirstOrDefault();
                if (arrayItemAttr != null)
                {
                    propertyName = (arrayItemAttr as XmlArrayItemAttribute)?.ElementName;
                }
            }
            writer.WriteStartElement(propertyName);
            if (o != null)
            {
                foreach (var key in dic.Keys)
                {
                    var  obj        = dic[key];
                    Type t          = obj.GetType();
                    var  type       = TypeUtility.CheckType(t);
                    var  typeWriter = TypeWriterFactory.Create(type);
                    typeWriter.ObjectToXmlNode(obj, writer, new CustomPropertyInfo(key.ToString(), t));
                }
            }
            return(writer);
        }
Ejemplo n.º 2
0
        public string Serialize(object o, bool hasXmlHeader = false, bool formatted = false)
        {
            string xml = null;

            using (MemoryStream ms = new MemoryStream())
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                //要求缩进
                settings.Indent = formatted;
                //注意如果不设置encoding默认将输出utf-16
                //注意这儿不能直接用Encoding.UTF8如果用Encoding.UTF8将在输出文本的最前面添加4个字节的非xml内容
                settings.Encoding = new UTF8Encoding(false);
                using (XmlWriter writer = XmlWriter.Create(ms, settings))
                {
                    if (hasXmlHeader)
                    {
                        writer.WriteStartDocument();
                    }
                    var    t        = o.GetType();
                    var    rootAttr = t.GetCustomAttributes(typeof(XmlRootAttribute), true).FirstOrDefault();
                    string root     = t.Name;
                    if (rootAttr != null)
                    {
                        root = (rootAttr as XmlRootAttribute).ElementName;
                    }
                    if (root.Contains("`"))
                    {
                        root = root.Substring(0, root.IndexOf("`"));
                    }
                    var type       = TypeUtility.CheckType(t);
                    var typeWriter = TypeWriterFactory.Create(type);
                    typeWriter.ObjectToXmlNode(o, writer, new CustomPropertyInfo(root, t));
                    if (hasXmlHeader)
                    {
                        writer.WriteEndDocument();
                    }
                }
                xml = Encoding.UTF8.GetString(ms.ToArray());
                if (!hasXmlHeader && xml.IndexOf("?>") > -1)
                {
                    xml = xml.Remove(0, xml.IndexOf("?>") + 2);
                }
            }
            return(xml);
        }
Ejemplo n.º 3
0
        public XmlWriter ObjectToXmlNode(object o, XmlWriter writer, PropertyInfo property)
        {
            string propertyName = property.Name;
            bool   pass         = false;
            var    attr         = property.GetCustomAttributes(typeof(XmlAttributeAttribute), true)?.FirstOrDefault();

            if (attr != null)
            {
                propertyName = (attr as XmlAttributeAttribute).AttributeName;
            }
            else
            {
                var arrayItemAttr = property.GetCustomAttributes(typeof(XmlArrayItemAttribute), true)?.FirstOrDefault();
                if (arrayItemAttr != null)
                {
                    propertyName = (arrayItemAttr as XmlArrayItemAttribute)?.ElementName;
                }
            }
            if (o == null)
            {
                return(writer);
            }
            var t = o.GetType();

            pass = t.GetCustomAttributes(typeof(XmlPass), true)?.FirstOrDefault() != null;
            if (!pass)
            {
                writer.WriteStartElement(propertyName);
            }
            var properties = t.GetProperties(BindingFlags.Instance | BindingFlags.Public);

            foreach (var p in properties)
            {
                var pObj       = p.GetValue(o, null);
                var type       = TypeUtility.CheckType(p.PropertyType);
                var typeWriter = TypeWriterFactory.Create(type);
                typeWriter.ObjectToXmlNode(pObj, writer, p);
            }
            if (!pass)
            {
                writer.WriteEndElement();
            }
            return(writer);
        }
Ejemplo n.º 4
0
        public XmlWriter ObjectToXmlNode(object o, XmlWriter writer, PropertyInfo property)
        {
            if (o == null)
            {
                return(writer);
            }
            bool   pass          = false;
            string propertyName  = property.Name;
            var    arrayAttr     = property.GetCustomAttributes(typeof(XmlArrayAttribute), true)?.FirstOrDefault();
            var    arrayItemAttr = property.GetCustomAttributes(typeof(XmlArrayItemAttribute), true)?.FirstOrDefault();
            string arrayItemName = null;

            if (arrayAttr != null)
            {
                propertyName = (arrayAttr as XmlArrayAttribute)?.ElementName;
            }
            if (arrayItemAttr != null)
            {
                arrayItemName = (arrayItemAttr as XmlArrayItemAttribute)?.ElementName;
            }
            var list = o as IList;

            pass = property.GetCustomAttributes(typeof(XmlPass), true)?.FirstOrDefault() != null;
            if (!pass)
            {
                writer.WriteStartElement(propertyName);
            }

            foreach (var l in list)
            {
                Type itemType   = l.GetType();
                var  type       = TypeUtility.CheckType(itemType);
                var  typewriter = TypeWriterFactory.Create(type);
                typewriter.ObjectToXmlNode(l, writer, property);
            }
            if (!pass)
            {
                writer.WriteEndElement();
            }
            return(writer);
        }