using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
namespace XmlToClass
{
public class XmlToClass
{
#region const
private const string _type = "#type#";
private const string _field = "#field#";
private const string _property = "#property#";
private const string _namespace = "#namespace#";
private const string _class = "#class#";
private const string property = @"
private #type# _#field#;
public #type# #property#
{
get{ return _#field#; }
set{ _#field# = value; }
}
";
private const string filetextstart = @"
using System;
using System.Text;
using System.Collections.Generic;
namespace #namespace#.Model
{
public class #class#
{";
private const string filetextend = @"
}
}";
#endregion
#region Properties
public Dictionary<string, string> Classes = new Dictionary<string, string>();
public string Namespace { get; set; }
public string SavePath { get; set; }
#endregion
public object ToClass(string xml)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
foreach (XmlNode xn in doc.ChildNodes)
{
this.ToProperty(xn);
}
return null;
}
//如果有子节点(排除#text)
//创建类
//如果子节点多个且子节点相同
//List<T>,类型名为子节点名
//属性 = 进入第一个子节点
//属性添加到类
//否则
//类型名为本节点名
//循环所有子节点
//{
//属性 = 进入子节点
//属性添加到类
//}
//保存
//否则创建普通属性
public string ToProperty(XmlNode node)
{
string type = string.Empty;
string result = string.Empty;
bool isList = false;
if (node != null)
{
if (node.Attributes != null && node.Attributes.Count > 0 || (node.HasChildNodes && node.FirstChild.Name != "#text"))
{
string start = XmlToClass.filetextstart.Replace(XmlToClass._namespace, this.Namespace);
StringBuilder classfile = new StringBuilder();
if (node.ChildNodes.Count > 1 && node.ChildNodes[0].Name == node.ChildNodes[1].Name)
{
isList = true;
type = node.ChildNodes[0].Name;
start = start.Replace(XmlToClass._class, type);
classfile.Append(start);
string str = this.ToProperty(node.ChildNodes[0]);
classfile.Append(str);
classfile.Append(XmlToClass.filetextend);
}
else
{
type = node.Name;
start = start.Replace(XmlToClass._class, type);
classfile.Append(start);
//xmlnode
foreach (XmlNode xn in node.ChildNodes)
{
string str = this.ToProperty(xn);
classfile.Append(str);
}
//XmlAttribute
foreach (XmlAttribute xa in node.Attributes)
{
string str = this.ToProperty(xa);
classfile.Append(str);
}
classfile.Append(XmlToClass.filetextend);
//保存
this.Classes.Add(type, classfile.ToString());
}
}
else
{
type = this.GetValueType(node.InnerText);
}
if (isList)
type = "List<" + type + ">";
result = this.ToProperty(node.Name, type);
}
return result;
}
public string ToProperty(XmlAttribute xa)
{
string type = string.Empty;
type = this.GetValueType(xa.InnerText);
return this.ToProperty(xa.Name, type);
}
public string GetValueType(string innerText)
{
string type = string.Empty;
#region getType
if (string.IsNullOrWhiteSpace(innerText))
type = "string";
else if (innerText.ToLower() == "true" || innerText.ToLower() == "false")
{
//bool
type = "bool";
}
else
{
try
{
try
{
try
{
//int32
Convert.ToInt32(innerText);
type = "int";
}
catch
{
//long
Convert.ToInt64(innerText);
type = "long";
}
}
catch
{
//double
Convert.ToDouble(innerText);
type = "double";
}
}
catch
{
//string
type = "string";
}
}
#endregion
return type;
}
public string ToProperty(string name, string type)
{
string result = string.Empty;
result = XmlToClass.property.Replace(XmlToClass._type, type);
result = result.Replace(XmlToClass._field, name.ToLower());
result = result.Replace(XmlToClass._property, name);
return result;
}
public void SaveToProject()
{
string model = "Model";
string extension = ".cs";
if (!string.IsNullOrWhiteSpace(this.SavePath))
{
string direc = Path.Combine(this.SavePath, model);
if (!Directory.Exists(direc))
Directory.CreateDirectory(direc);
foreach (string key in this.Classes.Keys)
{
using (FileStream fs = new FileStream(Path.Combine(direc, key + extension), FileMode.Create))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(this.Classes[key]);
}
}
}
}
}
}
}
438



被折叠的 条评论
为什么被折叠?



