将XML转换为实体
需求
将XML文件中的数据经过转换后插入到数据库中。
参考
C#实体类和XML的相互转换
https://blog.csdn.net/pan_junbiao/article/details/82938027
遇到的问题
错误描述
XML反序列化出错,XML 文档(2, 2)中有错误
解决方案
在实体类的字段要加上XmlElement属性
https://www.cnblogs.com/wuyunblog/p/6625747.html
具体实例
实体类和XML转换的帮助类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 using System.Xml.Serialization; 8 9 namespace conDealXML 10 { 11 public static class XmlSerializeHelper 12 { 13 14 /// <summary> 15 /// 将实体对象转换成XML 16 /// </summary> 17 /// <typeparam name="T">实体类型</typeparam> 18 /// <param name="obj">实体对象</param> 19 public static string XmlSerialize<T>(T obj) 20 { 21 try 22 { 23 using (StringWriter sw = new StringWriter()) 24 { 25 Type t = obj.GetType(); 26 XmlSerializer serializer = new XmlSerializer(obj.GetType()); 27 serializer.Serialize(sw, obj); 28 sw.Close(); 29 return sw.ToString(); 30 } 31 } 32 catch (Exception ex) 33 { 34 throw new Exception("将实体对象转换成XML异常", ex); 35 } 36 } 37 38 /// <summary> 39 /// 将XML转换成实体对象 40 /// </summary> 41 /// <typeparam name="T">实体类型</typeparam> 42 /// <param name="strXML">XML</param> 43 public static T DESerializer<T>(string strXML) where T : class 44 { 45 try 46 { 47 using (StringReader sr = new StringReader(strXML)) 48 { 49 XmlSerializer serializer = new XmlSerializer(typeof(T)); 50 return serializer.Deserialize(sr) as T; 51 } 52 } 53 catch (Exception ex) 54 { 55 throw new Exception("将XML转换成实体对象异常", ex); 56 } 57 58 } 59 } 60 }
实体类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Xml.Serialization; 3 namespace Model 4 { 5 /// <summary> 6 /// 功能: 实体类 (权限) 7 /// 创建人:CodeSmith 8 /// 创建日期:2019/12/25 9 /// </summary> 10 [Serializable] 11 [XmlRoot(ElementName = "Action")] 12 public partial class Action 13 { 14 public Action() 15 { } 16 #region Model 17 /// <summary> 18 /// 权限ID 19 /// </summary> 20 public short? ActionID {get; set;} 21 /// <summary> 22 /// 权限名字 23 /// </summary> 24 public string Name {get; set;} 25 /// <summary> 26 /// 权限标志代码(用于功能的判定) 27 /// </summary> 28 public string Code {get; set;} 29 /// <summary> 30 /// 权限的路径 31 /// </summary> 32 public string Url {get; set;} 33 /// <summary> 34 /// 排序 35 /// </summary> 36 public short? Sort {get; set;} 37 /// <summary> 38 /// 权限类型 39 /// </summary> 40 public int? ActionType {get; set;} 41 /// <summary> 42 /// 备注 43 /// </summary> 44 public string Memo {get; set;} 45 /// <summary> 46 /// 审核状态 47 /// </summary> 48 public short? Check {get; set;} 49 /// <summary> 50 /// 添加时间 51 /// </summary> 52 public DateTime? InsertTime {get; set;} 53 /// <summary> 54 /// 修改时间 55 /// </summary> 56 public DateTime? ModifyTime {get; set;} 57 #endregion 58 } 59 }
XML文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="gb2312"?> 2 <MyConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 3 <Roles> 4 <Role> 5 <Actions> 6 <Action> 7 <ActionID>133</ActionID> 8 <Name>用户查询</Name> 9 <Code>041303</Code> 10 <Url>/AddValue/addvalue_list</Url> 11 <Sort>4</Sort> 12 <ActionType>0</ActionType> 13 <Check>1</Check> 14 <InsertTime>2019-12-24T11:18:00</InsertTime> 15 <ModifyTime xsi:nil="true" /> 16 </Action> 17 </Actions> 18 <Menus> 19 <Menu> 20 <MenuID>165</MenuID> 21 <Title>用户管理</Title> 22 <ParentID>160</ParentID> 23 <Layer>4</Layer> 24 <Path>0/1/3/160</Path> 25 <Sort>12</Sort> 26 <Url>http://</Url> 27 <Image>http://</Image> 28 <IsVip>false</IsVip> 29 <MenuType>0</MenuType> 30 <Level>0</Level> 31 <Check>1</Check> 32 <InsertTime>2019-12-21T09:21:00</InsertTime> 33 <ModifyTime>2019-12-21T09:23:00</ModifyTime> 34 </Menu> 35 </Menus> 36 <RoleID></RoleID> 37 <Name>超级管理员</Name> 38 </Role> 39 <Role> 40 </Role> 41 </Roles> 42 </MyConfig>
控制台程序,执行转换的过程
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Xml; 7 8 namespace conDealXML 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 XmlDocument doc = new XmlDocument(); 15 doc.Load("Roles.Config"); 16 17 XmlNode xn_MyConfig = doc.SelectSingleNode("MyConfig"); 18 XmlNodeList xnl_Roles = xn_MyConfig.ChildNodes; 19 XmlNode xn_Roles_Roles = xnl_Roles[0]; 20 XmlNodeList xnl_Roles_Role = xn_Roles_Roles.ChildNodes; 21 XmlNode xn_Role_Admin = xnl_Roles_Role[0];//第一个Role,为超级管理员 22 XmlNodeList xnl_Role_Admin = xn_Role_Admin.ChildNodes; 23 foreach (XmlNode item in xnl_Role_Admin) 24 { 25 if (item.Name == "Actions") 26 { 27 XmlNodeList xnl_Actions = item.ChildNodes;//获取到所有的Action 28 foreach (XmlNode xn in xnl_Actions) 29 { 30 Model.Action model = XmlSerializeHelper.DESerializer<Model.Action>(xn.OuterXml); 31 new DAL.Action().Add(model); 32 } 33 } 34 if (item.Name == "Menus") 35 { 36 XmlNodeList xnl_Menus = item.ChildNodes;//获取到所有的Action 37 foreach (XmlNode xn in xnl_Menus) 38 { 39 Model.Menu model = XmlSerializeHelper.DESerializer<Model.Menu>(xn.OuterXml); 40 new DAL.Menu().Add(model); 41 } 42 } 43 } 44 45 Console.ReadKey(); 46 } 47 } 48 }