C#實體類生成XML與XML Schema文檔 -开发者知识库

C#實體類生成XML與XML Schema文檔 -开发者知识库,第1张

一、實體類生成XML

 1 private void CreateXML()
 2         {
 3             Type[] objType = DBEntityRegst();
 4             foreach (var item in objType)
 5             {
 6                 if (item == null)
 7                 {
 8                     break;
 9                 }
10 
11                 XmlDocument doc = new XmlDocument();
12 
13                 //<?xml version="1.0"?>
14                 XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "", "");
15                 doc.AppendChild(dec);
16 
17                 //創建一個根節點
18                 //<schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
19                 XmlElement root = doc.CreateElement("schema");
20                 root.SetAttribute("xmlns:xs", "http://www.w3.org/2001/XMLSchema");
21                 doc.AppendChild(root);
22 
23                 //<element Name="DTO_BatchTotal">
24                 XmlElement element1 = doc.CreateElement("element");
25                 element1.SetAttribute("Name", (item.Name).ToString());
26 
27                 //<complexType>
28                 XmlElement element2 = doc.CreateElement("complexType");
29 
30                 //<sequence>
31                 XmlElement element3 = doc.CreateElement("sequence");
32 
33 
34                 System.Reflection.PropertyInfo[] ps = item.GetProperties();
35                 for (int i = 0; i < ps.Length; i  )
36                 {
37                     string typeold = (ps[i].PropertyType).ToString();
38                     string[] a = typeold.Split('.');
39                     string typenew = ("xs:"   a[1]).ToLower();
40 
41                     //<element Name="batch" Type="xs:string" />
42                     //<element Name="total" Type="xs:decimal" />
43                     XmlElement element4 = doc.CreateElement("element");
44                     element4.SetAttribute("Name", ps[i].Name);
45                     element4.SetAttribute("Type", typenew);
46 
47                     //給element3添加子節點
48                     element3.AppendChild(element4);
49                 }
50                 //同上
51                 element1.AppendChild(element2);
52                 element2.AppendChild(element3);
53                 root.AppendChild(element1);
54 
55                 XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
56                 xsn.Add("xs", "http://www.w3.org/2001/XMLSchema");
57                 doc.Save(@"E:\XML\"   item.Name.ToString()   ".xml");
58 
59 
60             }
61         }
62 
63         /// <summary>
64         /// 將DTO實體加載到List列表
65         /// </summary>
66         private Type[] DBEntityRegst()
67         {
68 
69             List<IEntityRegstHelper> entityRegstHelperlist = null;
70             entityRegstHelperlist = new List<IEntityRegstHelper>();
71             var classes = Assembly.Load("ZxSoftERPGC.Model").GetTypes();//命名空間[Model]下的類類型集
72 
73             Type[] a = new Type[classes.Length];
74             int i = 0;
75             foreach (var classtype in classes)
76             {
77 
78                 if (((classtype.Name).ToString()).Contains("DTO_")) //指定獲取類名包含“DTO_”的類
79                 {
80                     a[i] = classtype;
81                     i  ;
82                 }
83             }
84             return a;
85         }

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复