zoukankan      html  css  js  c++  java
  • 匿名对象序列化为XML

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    namespace Xml.Utils
    {
        /// <summary>
        /// 匿名对象序列化为XML
        /// </summary>
        public static class XmlTools
        {
            //识别需要序列化的类型
            private static readonly Type[] WriteTypes = new[] {
            typeof(string), typeof(DateTime), typeof(Enum), 
            typeof(decimal?), typeof(Guid),typeof(int?)
        };
            public static bool IsSimpleType(this Type type)
            {
                return type.IsPrimitive || WriteTypes.Contains(type);
            }
            public static XElement ToXml(this object input)
            {
                return input.ToXml(null);
            }
            public static XElement ToXml(this object input, string element)
            {
                if (input == null)
                    return null;
                if (string.IsNullOrEmpty(element))
                    element = "object";
                element = XmlConvert.EncodeName(element);
                var ret = new XElement(element);
                if (input != null)
                {
                    var type = input.GetType();
                    var props = type.GetProperties();
                    var elements = from prop in props
                                   let name = XmlConvert.EncodeName(prop.Name)
                                   let val = prop.GetValue(input, null)
                                   let value = prop.PropertyType.IsSimpleType()
                                        ? new XElement(name, val)
                                        : val.ToXml(name)
                                   where value != null
                                   select value;
                    ret.Add(elements);
                }
                return ret;
            }
        }
    }
    

      

    class Program
    {
        static void Main(string[] args)
        {
            var me = new
            {
                Hello = "World",
                Other = new
                {
                    My = "Object",
                    V = 1,
                    B = (byte)2
                }
            };
    
            var x = me.ToXml();
        }
    }
    public static class Tools
    {
        private static readonly Type[] WriteTypes = new[] {
            typeof(string), typeof(DateTime), typeof(Enum), 
            typeof(decimal), typeof(Guid),
        };
        public static bool IsSimpleType(this Type type)
        {
            return type.IsPrimitive || WriteTypes.Contains(type);
        }
        public static XElement ToXml(this object input)
        {
            return input.ToXml(null);
        }
        public static XElement ToXml(this object input, string element)
        {
            if (input == null)
                return null;
    
            if (string.IsNullOrEmpty(element))
                element = "object";
            element = XmlConvert.EncodeName(element);
            var ret = new XElement(element);
    
            if (input != null)
            {
                var type = input.GetType();
                var props = type.GetProperties();
    
                var elements = from prop in props
                               let name = XmlConvert.EncodeName(prop.Name)
                               let val = prop.GetValue(input, null)
                               let value = prop.PropertyType.IsSimpleType()
                                    ? new XElement(name, val)
                                    : val.ToXml(name)
                               where value != null
                               select value;
    
                ret.Add(elements);
            }
    
            return ret;
        }
    

      http://stackoverflow.com/questions/2404685/can-i-serialize-anonymous-types-as-xml

  • 相关阅读:
    十七 SpringCloud Alibaba入门简介
    十六、Spring Cloud Sleuth 分布式请求链路追踪
    Linux下安装mongo
    MongoDB零基础入门到高级进阶-尚学堂
    十五、Spring Cloud Stream 消息驱动
    十四、SpringCloud Bus 消息总线
    十二、Gateway新一代网关
    十一、Zuul路由网关
    十、Hystrix熔断器(三)服务熔断
    十、Hystrix 断路器(二)服务降级
  • 原文地址:https://www.cnblogs.com/xiangxiong/p/6879145.html
Copyright © 2011-2022 走看看