c#生成xml文件並保存到本地


c#生成xml文件並保存到本地

本文主要是演示實體類Modal轉化為XML,使用了反射機制(PropertyInfo)。

添加命名空間:

using System.Xml;
using System.Reflection;

 

方法1:最原始,最基本的一種:利用XmlDocument向一個XML文件里寫節點,然后再利用XmlDocument保存文件

 

     /// <summary>  
        ///  實體類序列化成xml  
        /// </summary>  
        /// <param name="enitities">實體.</param>  
        /// <param name="headtag">根節點名稱</param>  
        public static void ObjListToXml<T>(List<T> enitities, string headtag) where T : new()
        {//方法一
            XmlDocument xmldoc = new XmlDocument();
            XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "iso-8859-1", null);//生成<?xml version="1.0" encoding="iso-8859-1"?>
            xmldoc.AppendChild(xmldecl);
            XmlElement modelNode = xmldoc.CreateElement("Users");
            xmldoc.AppendChild(modelNode);

            foreach (T entity in enitities)
            {
                if (entity != null)
                {
                    XmlElement childNode = xmldoc.CreateElement(entity.GetType().Name);
                    modelNode.AppendChild(childNode);
                    foreach (PropertyInfo property in entity.GetType().GetProperties())
                    {
                        XmlElement attritude = xmldoc.CreateElement(property.Name);
                        if (property.GetValue(entity, null) != null)
                        {
                            attritude.InnerText = property.GetValue(entity, null).ToString();
                        }
                        else
                        {
                            attritude.InnerText = "[NULL]";
                        }
                        childNode.AppendChild(attritude);
                    }
                }
            }
string file = "C:/users.xml";
       xmldoc.Save(file);
} 

 

 方法2:使用StringBuilder拼接XML,然后在使用XmlDocument的LoadXml(xml格式的字符串)轉換為xml文件,再保存到本地

/// <summary>  
        ///  實體類序列化成xml  
        /// </summary>  
        /// <param name="enitities">實體.</param>  
        /// <param name="headtag">根節點名稱</param>  
        public static void ObjListToXml<T>(List<T> enitities, string headtag) where T : new()
        {
            //方法二
            StringBuilder sb = new StringBuilder();
            PropertyInfo[] propinfos = null;
            sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            sb.AppendLine("<" + headtag + ">");
            foreach (T obj in enitities)
            {
                //初始化propertyinfo  
                if (propinfos == null)
                {
                    Type objtype = obj.GetType();
                    propinfos = objtype.GetProperties();
                }
                sb.AppendLine("<" + obj.GetType().Name + ">");
                foreach (PropertyInfo propinfo in propinfos)
                {
                    sb.Append("<");
                    sb.Append(propinfo.Name);
                    sb.Append(">");
                    sb.Append(propinfo.GetValue(obj, null));
                    sb.Append("</");
                    sb.Append(propinfo.Name);
                    sb.AppendLine(">");
                }
                sb.AppendLine("</" + obj.GetType().Name + ">");
            }
            sb.AppendLine("</" + headtag + ">");
       XmlDocument xmldoc = new XmlDocument();
       xmldoc.LoadXml(sb.ToString());
       string file = "C:/users.xml";
       xmldoc.Save(file);

}

 

本人為大三學生,以上僅是實習總結,僅供大家參考,如有更好的方法,歡迎大牛們指出。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM