
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Text; 6 using System.Xml; 7 8 namespace XmlToClass 9 { 10 public class XmlToClass 11 { 12 #region const 13 private const string _type = "#type#"; 14 private const string _field = "#field#"; 15 private const string _property = "#property#"; 16 private const string _namespace = "#namespace#"; 17 private const string _class = "#class#"; 18 19 private const string property = @" 20 private #type# _#field#; 21 public #type# #property# 22 { 23 get{ return _#field#; } 24 set{ _#field# = value; } 25 } 26 "; 27 28 private const string filetextstart = @" 29 using System; 30 using System.Text; 31 using System.Collections.Generic; 32 namespace #namespace#.Model 33 { 34 public class #class# 35 {"; 36 37 private const string filetextend = @" 38 } 39 }"; 40 #endregion 41 42 #region Properties 43 public Dictionary<string, string> Classes = new Dictionary<string, string>(); 44 public string Namespace { get; set; } 45 public string SavePath { get; set; } 46 #endregion 47 48 public object ToClass(string xml) 49 { 50 XmlDocument doc = new XmlDocument(); 51 doc.LoadXml(xml); 52 53 foreach (XmlNode xn in doc.ChildNodes) 54 { 55 this.ToProperty(xn); 56 } 57 58 return null; 59 } 60 61 62 //如果有子节点(排除#text) 63 //创建类 64 //如果子节点多个且子节点相同 65 //List<T>,类型名为子节点名 66 //属性 = 进入第一个子节点 67 //属性添加到类 68 //否则 69 //类型名为本节点名 70 //循环所有子节点 71 //{ 72 //属性 = 进入子节点 73 //属性添加到类 74 //} 75 //保存 76 //否则创建普通属性 77 78 public string ToProperty(XmlNode node) 79 { 80 string type = string.Empty; 81 string result = string.Empty; 82 bool isList = false; 83 84 if (node != null) 85 { 86 if (node.Attributes != null && node.Attributes.Count > 0 || (node.HasChildNodes && node.FirstChild.Name != "#text")) 87 { 88 string start = XmlToClass.filetextstart.Replace(XmlToClass._namespace, this.Namespace); 89 StringBuilder classfile = new StringBuilder(); 90 91 if (node.ChildNodes.Count > 1 && node.ChildNodes[0].Name == node.ChildNodes[1].Name) 92 { 93 isList = true; 94 type = node.ChildNodes[0].Name; 95 start = start.Replace(XmlToClass._class, type); 96 classfile.Append(start); 97 string str = this.ToProperty(node.ChildNodes[0]); 98 classfile.Append(str); 99 classfile.Append(XmlToClass.filetextend); 100 } 101 else 102 { 103 type = node.Name; 104 start = start.Replace(XmlToClass._class, type); 105 classfile.Append(start); 106 //xmlnode 107 foreach (XmlNode xn in node.ChildNodes) 108 { 109 string str = this.ToProperty(xn); 110 classfile.Append(str); 111 } 112 //XmlAttribute 113 foreach (XmlAttribute xa in node.Attributes) 114 { 115 string str = this.ToProperty(xa); 116 classfile.Append(str); 117 } 118 classfile.Append(XmlToClass.filetextend); 119 //保存 120 this.Classes.Add(type, classfile.ToString()); 121 } 122 } 123 else 124 { 125 type = this.GetValueType(node.InnerText); 126 } 127 if (isList) 128 type = "List<" + type + ">"; 129 result = this.ToProperty(node.Name, type); 130 } 131 132 return result; 133 } 134 135 public string ToProperty(XmlAttribute xa) 136 { 137 string type = string.Empty; 138 type = this.GetValueType(xa.InnerText); 139 return this.ToProperty(xa.Name, type); 140 } 141 142 public string GetValueType(string innerText) 143 { 144 string type = string.Empty; 145 #region getType 146 if (string.IsNullOrWhiteSpace(innerText)) 147 type = "string"; 148 else if (innerText.ToLower() == "true" || innerText.ToLower() == "false") 149 { 150 //bool 151 type = "bool"; 152 } 153 else 154 { 155 try 156 { 157 try 158 { 159 try 160 { 161 //int32 162 Convert.ToInt32(innerText); 163 type = "int"; 164 } 165 catch 166 { 167 //long 168 Convert.ToInt64(innerText); 169 type = "long"; 170 } 171 } 172 catch 173 { 174 //double 175 Convert.ToDouble(innerText); 176 type = "double"; 177 } 178 } 179 catch 180 { 181 //string 182 type = "string"; 183 } 184 } 185 #endregion 186 return type; 187 } 188 189 public string ToProperty(string name, string type) 190 { 191 string result = string.Empty; 192 result = XmlToClass.property.Replace(XmlToClass._type, type); 193 result = result.Replace(XmlToClass._field, name.ToLower()); 194 result = result.Replace(XmlToClass._property, name); 195 return result; 196 } 197 198 public void SaveToProject() 199 { 200 string model = "Model"; 201 string extension = ".cs"; 202 if (!string.IsNullOrWhiteSpace(this.SavePath)) 203 { 204 string direc = Path.Combine(this.SavePath, model); 205 if (!Directory.Exists(direc)) 206 Directory.CreateDirectory(direc); 207 foreach (string key in this.Classes.Keys) 208 { 209 using (FileStream fs = new FileStream(Path.Combine(direc, key + extension), FileMode.Create)) 210 { 211 using (StreamWriter sw = new StreamWriter(fs)) 212 { 213 sw.WriteLine(this.Classes[key]); 214 } 215 } 216 } 217 } 218 } 219 } 220 }