根据xml文件名获取xml数据并转化为实体。

1.定义一个xml文件。

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <UserManager xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
 3   <User>
 4     <UserName>王三</UserName>
 5     <Age>18</Age>
 6     <Gender>male</Gender>
 7   </User>
 8   <User>
 9     <UserName>李四</UserName>
10     <Age>18</Age>
11     <Gender>male</Gender>
12   </User>
13   <User>
14     <UserName>王五</UserName>
15     <Age>18</Age>
16     <Gender>male</Gender>
17   </User>
18 </UserManager>

2.定义Helper类。

 1  public class NickZHao
 2     {
 3         public static string GetXmlPath(string XmlName)
 4         {
 5             string filePath = string.Empty;
 6             filePath = System.Web.HttpContext.Current.Server.MapPath(string.Concat("", XmlName+".xml"));
 7 
 8             return filePath;
 9         }
10 
11         public static List<T> XmlToEntityList<T>(string XmlName)
12         {
13             string xmlPath = GetXmlPath(XmlName);
14             XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
15             Object obj = new Object();
16 
17             if (File.Exists(xmlPath))
18             {
19                 using (StreamReader reader = new StreamReader(xmlPath))
20                 {
21                     try
22                     {
23                         obj = serializer.Deserialize(reader);
24                     }
25                     catch (Exception ex)
26                     {
27                         //Here put your code witch dealing with exception
28                     }
29                 }
30             }
31             return (List<T>)obj;
32         }
33 
34         public static T XmlToEntity<T>(string XmlName)
35         {
36             string xmlPath = GetXmlPath(XmlName);
37             XmlSerializer serializer = new XmlSerializer(typeof(T));
38             Object obj = new Object();
39 
40             if (File.Exists(xmlPath))
41             {
42                 using (StreamReader reader = new StreamReader(xmlPath))
43                 {
44                      try
45                     {
46                         obj = serializer.Deserialize(reader);
47                     }
48                      catch (Exception ex)
49                      {
50                          //Here put your code witch dealing with exception
51                      }
52                 }
53             }
54             return (T)obj;
55         }  
56     }

3.调用

     UserManager userManager = NickZHao.XmlToEntity<UserManager>("User");

        [XmlRoot(ElementName = "UserManager")]
        public class UserManager 
        {
            [XmlElement(ElementName = "User")]
            public List<User> UserList { get; set; }

        }
        
        public class User
        {
            public string UserName { get; set; }

            public int Age { get; set; }

            public string Gender { get; set; }

        }

注意:在定义实体类是记得标记根节点

[XmlRoot(ElementName = "UserManager")]

和节点元素

[XmlElement(ElementName = "User")]
posted @ 2016-12-21 14:04  ~扎克伯格  阅读(608)  评论(0编辑  收藏  举报