public ExChangeXML Clone() { ExChangeXML rlt = MemberwiseClone() as ExChangeXML; return(rlt); }
/// <summary> /// XML转Datatable /// </summary> /// <param name="s"></param> /// <returns></returns> public static DataTable XML2Table(string s) { XmlDocument xdoc = new XmlDocument(); if (s != "") { xdoc.LoadXml(s); XmlNode root = xdoc.FirstChild; DataTable _dt = new DataTable(); if (root.Name == "Parameters") { using (DataTable dt = new DataTable()) { XmlNode node = root.FirstChild; ExChangeXML od = new ExChangeXML(); foreach (XmlAttribute a in node.Attributes) { if (a.Name != null) { dt.Columns.Add(a.Name); } } DataRow dataRow = dt.NewRow(); foreach (XmlAttribute a in node.Attributes) { Type type = od.GetType(); System.Reflection.PropertyInfo pi; if (a.Name != null) { pi = type.GetProperty(a.Name); if (pi != null) { if (pi.PropertyType.FullName == "System.Int32") { pi.SetValue(od, int.Parse(a.Value, null)); } else if (pi.PropertyType.FullName == "System.String") { pi.SetValue(od, a.Value, null); } else if (pi.PropertyType.FullName == "System.Int64") { pi.SetValue(od, long.Parse(a.Value), null); } } dataRow[pi.Name] = pi.GetValue(od, null); } } dt.Rows.Add(dataRow); return(dt); } } else if (root.Name == "Properties") { using (DataTable dt = new DataTable()) { DataRow dataRow = dt.NewRow(); Property po = new Property(); foreach (XmlNode n in root.ChildNodes) { Type type = po.GetType(); System.Reflection.PropertyInfo pi; if (n.Name != null) { dt.Columns.Add(n.Name); pi = type.GetProperty(n.Name); if (pi != null) { if (pi.PropertyType.FullName == "System.Int32") { pi.SetValue(po, int.Parse(n.InnerText, null)); } else if (pi.PropertyType.FullName == "System.String") { pi.SetValue(po, n.InnerText, null); } else if (pi.PropertyType.FullName == "System.Int64") { pi.SetValue(po, long.Parse(n.InnerText), null); } } dataRow[pi.Name] = pi.GetValue(po, null); } } dt.Rows.Add(dataRow); return(dt); } } else { return(null); } } else { return(null); } }