(List ls,string path) { XmlDocument xmldoc = new XmlDocument(); //加入XML的声明段落, XmlDeclaration xmldecl; xmldecl = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null); xmldoc.AppendChild(xmldecl); //加入一个根元素 var xmlelem = xmldoc.CreateElement("", "Nodes", ""); xmldoc.AppendChild(xmlelem); //加入另外一个元素 foreach (T s in ls) { XmlNode root = xmldoc.SelectSingleNode("Nodes"); XmlElement xe1 = xmldoc.CreateElement("Node"); foreach (PropertyInfo pi in typeof(T).GetProperties()) { object value = null; if (s != null) { value = pi.GetValue(s, null); XmlElement xesub1 = xmldoc.CreateElement(pi.Name); xesub1.InnerText = value == null ? "" : value.ToString(); xe1.AppendChild(xesub1); root.AppendChild(xe1); } } } //保存创建好的XML文档
xmldoc.Save(path); }
View Code
/// /// Xml生成实体集合 /// /// /// /// public static List getxmltomodel(string xmlpath) { try { #region 遍历xml XmlDocument _xm = new XmlDocument(); _xm.Load(xmlpath);
XmlNodeList xmList = _xm.SelectNodes("/pma_xml_export/database/table"); List _ls = new List(); foreach (XmlNode pageNode in xmList) {
T _m = Activator.CreateInstance(); foreach (PropertyInfo pi in typeof(T).GetProperties()) {
foreach (XmlNode nd in pageNode.ChildNodes) { XmlElement element = (XmlElement)nd; if (element.GetAttribute("name") == pi.Name) { object value = null; value = element.InnerText; Type type = pi.GetType(); pi.SetValue(_m, Convert.ChangeType(value, pi.PropertyType), null); } }
/// /// 获取表信息 /// /// /// public DataTable GetTableInfo(string tablename) { try { string sql = string.Format(@" SELECT 表名 = d.name,--case when a.colorder=1 then d.name else ‘‘ end, 表说明 = case when a.colorder=1 then isnull(f.value,‘‘) else ‘‘ end, 字段序号 = a.colorder, fieldname = a.name, 标识 = case when COLUMNPROPERTY( a.id,a.name,‘IsIdentity‘)=1 then ‘√‘else ‘‘ end, 主键 = case when exists(SELECT 1 FROM sysobjects where xtype=‘PK‘ and parent_obj=a.id and name in ( SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then ‘√‘ else ‘‘ end, sqltype = b.name, 占用字节数 = a.length, 长度 = COLUMNPROPERTY(a.id,a.name,‘PRECISION‘), 小数位数 = isnull(COLUMNPROPERTY(a.id,a.name,‘Scale‘),0), 允许空 = case when a.isnullable=1 then ‘√‘else ‘‘ end, 默认值 = isnull(e.text,‘‘), fieldexplain = isnull(g.[value],‘‘) FROM syscolumns a left join systypes b on a.xusertype=b.xusertype inner join sysobjects d on a.id=d.id and d.xtype=‘U‘ and d.name<>‘dtproperties‘ left join syscomments e on a.cdefault=e.id left join
sys.extended_properties g on a.id=g.major_id and a.colid=g.minor_id left join sys.extended_properties f on d.id=f.major_id and f.minor_id=0 where d.name=‘{0}‘ --如果只查询指定表,加上此条件 order by a.id,a.colorder", tablename); return Service.Data.SqlHelper.DataTable(sql); } catch (Exception ex) { throw (ex); } } }