程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#中實體集合和實體轉換成相應的string、XDocument、XElement、XDocument

C#中實體集合和實體轉換成相應的string、XDocument、XElement、XDocument

編輯:關於C#

一、前言

上篇隨筆主要是針對於Xml的特性Attribute與實體之間的匹配與轉換。該篇隨筆主要內容為將對象轉換成相應的Xml以及XElement。這2篇隨筆以不同的方式對Xml進行轉換與匹配,每種匹配都采用不同的角度進行操作。本文主要為對象實體的轉換,下篇側重於Xml的匹配。

二、Xml轉換

2.1 實體集合轉換Xml

實體集合轉換Xml的方法為:public static string ToXml<T>(IList<T> entities, string rootName = "") where T : new(),通過傳入的實體集合對象和Xml根名稱,可以轉換成相應的Xml,代碼如下:

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();
        }

針對於實體集合的轉換,轉換後的結果如下:

<?xml version="1.0" encoding="utf-8" ?>
<MapperInfoSet>
  <MapperInfo>
    <Name>MapperInfoIndex0</Name>
    <CreatedTime>2012-02-19T08:54:44.9411601+08:00</CreatedTime>
    <IsActive>true</IsActive>
    <Value>0</Value>
    <Percent>50</Percent>
    <TargetUrl>www.codeplex.com?Id=0</TargetUrl>
  </MapperInfo>
  <MapperInfo>
    <Name>MapperInfoIndex1</Name>
    <CreatedTime>2012-02-19T08:54:44.9421602+08:00</CreatedTime>
    <IsActive>false</IsActive>
    <Value>1</Value>
    <Percent>50</Percent>
    <TargetUrl>www.codeplex.com?Id=1</TargetUrl>
  </MapperInfo>
</MapperInfoSet>

2.2 實體轉換Xml

實體轉換Xml的方法為:public static string ToXml<T>(T entity) where T : new(),通過傳入的實體,可以轉換成相應的Xml,代碼如下:

public static string ToXml<T>(T entity) where T : new()
        {
            if (entity == null)
            {
                return string.Empty;
            }
    
            XElement element = ToXElement<T>(entity);
    
            return element.ToString();
        }

針對於單個實體的轉換,轉換後的結果如下:

<MapperInfo>
  <Name>MapperInfoIndex0</Name>
  <CreatedTime>2012-02-19T08:59:17.1387289+08:00</CreatedTime>
  <IsActive>true</IsActive>
  <Value>0</Value>
  <Percent>50</Percent>
  <TargetUrl>www.codeplex.com?Id=0</TargetUrl>
</MapperInfo>

2.3 實體集合轉換XElement

實體轉換XElement的方法為:public static XElement ToXElement<T>(IList<T> entities, string rootName = "") where T : new(),通過傳入的實體集合對象和Xml根名稱,可以轉換成相應的XElement,代碼如下:

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;
        }

2.4 實體集合轉換XmlDocument

實體轉換XmlDocument的方法為:public static XElement ToXmlDocument<T>(IList<T> entities, string rootName = "") where T : new(),通過傳入的實體集合對象和Xml根名稱,可以轉換成相應的XmlDocument,代碼如下:

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;
        }

2.5 實體轉換XElement

實體轉換XElement的方法為:public static string ToXElement<T>(T entity) where T : new(),通過傳入的實體,可以轉換成相應的XElement,代碼如下:

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;
        }

2.6 實體集合轉換XDocument

實體轉換XDocument的方法為:public static XDocument ToXDocument<T>(IList<T> entities, string rootName = "") where T : new(),通過傳入的實體集合對象和Xml根名稱,可以轉換成相應的XDocument,代碼如下:

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));
        }

三、總結

以上的代碼很少,主要通過重構來使代碼簡化。當然,將實體集合和實體轉換為相應的string、XDocument、XElement、XDocument是非常簡單的。單元測試的代碼就不貼了,占地方。下篇隨筆主要是如何將本文中轉換的Xml進行匹配,本文所有的代碼如下:

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;
        }
    }

作者:JasenKin

出處:http://www.cnblogs.com/jasenkin/

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved