C#實體類轉存入XML

來源:互聯網
上載者:User

標籤:

  這兩天,應要求做一個C/S的小程式,考慮到程式簡潔小巧,存資料的方式不使用資料庫,而是直接存入XML文檔中儲存。可以使用C#有的反射機制,做一個簡單的通用工具類來實現。

實作類別代碼:

using System;using System.Reflection;using System.Xml.Linq;namespace MyTool{    public static class ObjectToXml    {        public static string Path        {            get { return String.Format(@"{0}/{1}.xml", Environment.CurrentDirectory,DateTime.Now.Day.ToString()); }        }        public static void Intit(object ob)        {            XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),                new XElement("Data")); //建立跟節點 <Data></Data>            Process(ob,doc.Element("Data"));            doc.Save(Path);        }        private static void Process(object ob,XElement doc)        {            XElement data = doc;            string ClassName = ob.GetType().Name;            data.Add(new XElement(ClassName));//取類名 添加一個節點            PropertyInfo[] properties = ob.GetType().GetProperties(BindingFlags.Instance |BindingFlags.Public);            foreach (var item in properties)            {                string type = item.GetValue(ob).GetType().Name;                if (type.ToLower() == "string" || type.ToLower() == "int32" || type.ToLower() == "double")//判斷屬性是否是基本類型比如strin int double 若是,則建立節點並賦值                {                    XElement ChildNode = data.Element(ClassName);                    ChildNode.Add(new XElement(item.Name, item.GetValue(ob)));                }                else //否則,遞迴運行Process函數                {                    Process(item.GetValue(ob), data.Element(ClassName));                }            }        }    }}
View Code

使用:

using System;using MyTool;namespace xmltest{    class Program    {        static void Main(string[] args)        {            var a = new student            {                Name = "memeda", age = 10,gg=1.5,                more=new info                {                    boy="nan",                    moreinfo=new moreinfo                    {                        mv="mv"                    }                }            };            ObjectToXml.Intit(a);            Console.ReadKey();        }    }    public class student    {        public string Name { get; set; }        public int age { get; set; }        public double gg { get; set; }        public info more { get; set; }    }    public class info    {        public moreinfo moreinfo { get; set; }        public string boy { get; set; }    }    public class moreinfo    {        public string mv { get; set; }    }}

最後的效果:

C#實體類轉存入XML

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.