zoukankan      html  css  js  c++  java
  • C#中实体集合和实体转换成相应的string、XDocument、XElement

    C#中实体集合和实体转换成相应的string、XDocument、XElement

    public class SimpleXmlConverter
        {
            public static string ToXml<T>(IList<T> entities, string rootName = "") where T : new()
            {
                if (entities == null || entities.Count == 0)
                {
                    return string.Empty;
                }
        
                StringBuilder builder = new StringBuilder();
                builder.AppendLine(XmlResource.XmlHeader);
        
                XElement element = ToXElement<T>(entities, rootName);
                builder.Append(element.ToString());
        
                return builder.ToString();
            }
        
            public static XmlDocument ToXmlDocument<T>(IList<T> entities, string rootName = "") where T : new()
            {
                if (entities == null || entities.Count == 0)
                {
                    return null;
                }
        
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(ToXml<T>(entities, rootName));
        
                return xmlDocument;
            }
        
            public static XDocument ToXDocument<T>(IList<T> entities, string rootName = "") where T : new()
            {
                if (entities == null || entities.Count == 0)
                {
                    return null;
                }
                  
                return  XDocument.Parse(ToXml<T>(entities, rootName));
            }
        
            public static XElement ToXElement<T>(IList<T> entities, string rootName = "") where T : new()
            {
                if (entities == null || entities.Count == 0)
                {
                    return null;
                }
                     
                if (string.IsNullOrWhiteSpace(rootName))
                {
                    rootName = typeof(T).Name + XmlResource.XmlRootNameSuffix;
                }
        
                XElement element = new XElement(rootName);
        
                foreach (T entity in entities)
                {
                    element.Add(ToXElement<T>(entity));
                }
        
                return element;
            }
        
            public static string ToXml<T>(T entity) where T : new()
            {
                if (entity == null)
                {
                    return string.Empty;
                }
        
                XElement element = ToXElement<T>(entity);
        
                return element.ToString();
            }
        
            public static XElement ToXElement<T>(T entity) where T : new()
            {
                if (entity == null)
                {
                    return null;
                }
        
                XElement element = new XElement(typeof(T).Name);
                PropertyInfo[] properties = typeof(T).GetProperties();
                XElement innerElement = null;
                object propertyValue = null;
        
                foreach (PropertyInfo property in properties)
                {
                    propertyValue = property.GetValue(entity, null);
                    innerElement = new XElement(property.Name, propertyValue);
                    element.Add(innerElement);
                }
        
                return element;
            }
        
            public static XElement ToXElement(Type type) 
            {
                if (type == null)
                {
                    return null;
                }
        
                XElement element = new XElement(type.Name);
                PropertyInfo[] properties = type.GetProperties();
                XElement innerElement = null;
        
                foreach (PropertyInfo property in properties)
                {
                    innerElement = new XElement(property.Name, null);
                    element.Add(innerElement);
                }
        
                return element;
            }
        }
    View Code
  • 相关阅读:
    7. 输油管道问题
    6. 循环赛日程表
    4.JSP内置对象
    3.JSP
    2.CSS
    1.HTML
    5. 逆序对数
    [转]Android View.onMeasure方法的理解
    [转]android:clipToPadding和android:clipChildren
    [转]倍数提高工作效率的 Android Studio 奇技
  • 原文地址:https://www.cnblogs.com/love201314/p/5590709.html
Copyright © 2011-2022 走看看