public static DBPkg deserializePKG(string content) { MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(content)); XmlDocument doc = new XmlDocument(); doc.Load(ms); XmlNode root = doc.FirstChild; string stype = root.Attributes["type"].Value; object dbpkg = typeof(DBSerializer).Assembly.CreateInstance(stype); if (dbpkg != null) { DBPkg pkg = (DBPkg)dbpkg; pkg.guid = root.Attributes["guid"].Value; if (root.ChildNodes != null) { foreach (XmlNode child in root.ChildNodes) { string fieldname = child.Attributes["name"].Value; string filedcontent = child.Attributes["value"].Value; FieldInfo field = pkg.GetType().GetField(fieldname); object objfield = DBSerializer.deserializedField(field, filedcontent); field.SetValue(dbpkg, objfield); } } } return((DBPkg)dbpkg); }
public static string serializePKG(DBPkg pkg) { Console.WriteLine("serializePKG"); Type tp = pkg.GetType(); List <string> columns = DBSerializer.getDBColoumns(tp); Console.WriteLine("serializePKG" + columns.Count.ToString()); XmlDocument doc = new XmlDocument(); XmlElement root = doc.CreateElement("db_data"); doc.AppendChild(root); root.SetAttribute("type", tp.FullName); root.SetAttribute("guid", pkg.guid); for (int i = 0; i < columns.Count; ++i) { XmlElement argnode = doc.CreateElement("field"); root.AppendChild(argnode); FieldInfo field = tp.GetField(columns[i]); argnode.SetAttribute("type", field.FieldType.Name); argnode.SetAttribute("name", columns[i]); argnode.SetAttribute("value", DBSerializer.serializeField(field.FieldType, field.GetValue(pkg))); } System.IO.MemoryStream ms = new System.IO.MemoryStream(1024); doc.Save(ms); return(Encoding.UTF8.GetString(ms.ToArray())); }
public virtual void SyncClient(DBPkg pkg) { Console.WriteLine("sync: " + pkg.GetType().Name); string data = DBSerializer.serializePKG(pkg); Console.WriteLine("SyncClient: " + data); DB_SyncClient(mDBInterface.guid, data); }
protected void addChild <T>(string guid) where T : DBPkg { Debug.Assert(!string.IsNullOrEmpty(guid)); Type tp = typeof(T); DBPkg db = (DBPkg)tp.GetConstructor(Type.EmptyTypes).Invoke(null); Debug.Assert(db); FieldInfo info = tp.GetField("guid"); Debug.Assert(info != null); info.SetValue(db, guid); db.fetch(); mDBList.Add(db); }