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

}

 

本人为大三学生,以上仅是实习总结,仅供大家参考,如有更好的方法,欢迎大牛们指出。

posted on 2016-08-03 11:08  CarrieJ  阅读(7854)  评论(1编辑  收藏  举报