-
C#教程之C#教程之XML操作类
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Reflection; 5 using System.Text; 6 using System.Xml; 7 8 namespace CommonLib 9 { 10 public class XMLHelper 11 { 12 public XMLHelper() 13 { 14 15 } 16 17 public void Create<T>(string xmlPath, List<T> updateXMLs) 18 { 19 XmlDocument document = new XmlDocument(); 20 Type type = typeof(T); 21 document.AppendChild(document.CreateXmlDeclaration("1.0", "utf-8", null)); 22 XmlElement root = document.CreateElement(type.Name + "Collection"); 23 PropertyInfo[] properties = type.GetProperties(); 24 foreach (var updateXml in updateXMLs) 25 { 26 XmlElement parentElememt = document.CreateElement(type.Name); 27 foreach (PropertyInfo property in properties) 28 { 29 XmlElement element = document.CreateElement(property.Name); 30 element.InnerText = updateXml.GetPropertyValue(property).ToString(); 31 parentElememt.AppendChild(element); 32 } 33 root.AppendChild(parentElememt); 34 } 35 document.AppendChild(root); 36 document.Save(xmlPath); 37 } 38 39 public List<T> Read<T>(string updatexml) where T : new() 40 { 41 XmlDocument document = new XmlDocument(); 42 document.Load(updatexml); 43 List<T> tList = new List<T>(); 44 foreach (XmlNode node in document.ChildNodes[1].ChildNodes) 45 { 46 T t = new T(); 47 foreach (XmlNode childNode in node.ChildNodes) 48 { 49 t.SetPropertyValue(childNode.Name, childNode.InnerText); 50 } 51 tList.Add(t); 52 } 53 return tList; 54 } 55 56 57 public List<T> ReadContext<T>(string context) where T : new() 58 { 59 XmlDocument document = new XmlDocument(); 60 byte[] bytes = Encoding.UTF8.GetBytes(context); 61 MemoryStream ms = new MemoryStream(bytes); 62 ms.Seek(0, SeekOrigin.Begin); 63 document.Load(ms); 64 List<T> tList = new List<T>(); 65 foreach (XmlNode node in document.ChildNodes[1].ChildNodes) 66 { 67 T t = new T(); 68 foreach (XmlNode childNode in node.ChildNodes) 69 { 70 t.SetPropertyValue(childNode.Name, childNode.InnerText); 71 } 72 tList.Add(t); 73 } 74 return tList; 75 } 76 } 77 }
栏目列表
最新更新
如何使用OS模块中的stat方法
Python os 模块
seek() 方法
python打开文件实例1
Python写入文件
什么是流?
文件操作如何进制逐行读取
Python相对路径
with创建临时运行环境
Python文件操作
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
access教程之Access简介
mysql 安装了最新版本8.x版本后的报错:
Mysql空间数据&空间索引(spatial)
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
数据定义功能
数据操作功能