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

 

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

转载于:https://www.cnblogs.com/Carrie-J/p/5728161.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值