public static void InsertAll <T>(string path, Dictionary <int, T> dict, DataKeyType keyType) { XmlDocument xmlDoc = LoadDocument(path); FileStream fs = File.OpenRead(path); if (fs == null) { return; } if (dict == null) { return; } XmlNode xmlNode = xmlDoc.SelectSingleNode(ROOTNAME); Dictionary <int, T> .Enumerator em = dict.GetEnumerator(); while (em.MoveNext()) { XmlElement xe = xmlDoc.CreateElement(ELEMNAME); xmlNode.AppendChild(xe); int key = em.Current.Key; T obj = em.Current.Value; xe.SetAttribute(keyType.ToString(), key.ToString()); Save <T>(xe, obj); } em.Dispose(); fs.Close(); fs.Dispose(); xmlDoc.Save(path); }
public static void Delete(string path, int key, DataKeyType keyType) { XmlDocument xmlDoc = LoadDocument(path); FileStream fs = File.OpenRead(path); if (fs == null) { return; } XmlNodeList xmlNodeList = xmlDoc.SelectSingleNode(ROOTNAME).ChildNodes; XmlElement xe = null; for (int i = 0; i < xmlNodeList.Count; i++) { xe = xmlNodeList.Item(i) as XmlElement; if (xe == null) { continue; } string id = xe.GetAttribute(keyType.ToString()); if (int.Parse(id) == key) { XmlNode node = xmlDoc.SelectSingleNode(ROOTNAME); node.RemoveChild(xe); break; } } fs.Close(); fs.Dispose(); xmlDoc.Save(path); }
public static void Update(string path, int key, object obj, DataKeyType keyType) { XmlDocument xmlDoc = LoadDocument(path); FileStream fs = File.OpenRead(path); if (fs == null) { return; } XmlNodeList xmlNodeList = xmlDoc.SelectSingleNode(ROOTNAME).ChildNodes; for (int i = 0; i < xmlNodeList.Count; i++) { XmlElement xe = xmlNodeList.Item(i) as XmlElement; if (xe == null) { continue; } string id = xe.GetAttribute(keyType.ToString()); if (id.ToInt32() == key) { Save(xe, obj); break; } } fs.Close(); fs.Dispose(); xmlDoc.Save(path); }
public static void Append(string path, int key, object obj, DataKeyType keyType) { XmlDocument xmlDoc = LoadDocument(path); FileStream fs = File.OpenRead(path); if (fs == null) { return; } XmlNode xmlNode = xmlDoc.SelectSingleNode(ROOTNAME); XmlElement em = xmlDoc.CreateElement(ELEMNAME); xmlNode.AppendChild(em); em.SetAttribute(keyType.ToString(), key.ToString()); Save(em, obj); fs.Close(); fs.Dispose(); xmlDoc.Save(path); }
public static DataKeyType GetPkType(string pkType) { DataKeyType type = (DataKeyType)Enum.Parse(typeof(DataKeyType), pkType); return(type); }