static void Main(string[] args) { //模拟从数据库加载索引到内存中,形成内存中的数据库 //这里的 "Dictionary" 用来表达“一个用户注册过多少店铺“,即UserID与ShopID的一对多关系 SerializableDictionary <int, List <int> > dic = new SerializableDictionary <int, List <int> >(); List <int> shopIDList = new List <int>(); for (int shopID = 300000; shopID < 300050; shopID++) { shopIDList.Add(shopID); } int UserID = 15; //假设这里已经维护好了UserID与ShopID的关系 dic.Add(UserID, shopIDList); XmlSerializer xml = new XmlSerializer(dic.GetType()); var memoryStream = new MemoryStream(); xml.Serialize(memoryStream, dic); memoryStream.Seek(0, SeekOrigin.Begin); //将Dictionary持久化,相当于模拟保存在Mencache里面 File.AppendAllText("F://1.txt", Encoding.UTF8.GetString(memoryStream.ToArray())); Console.WriteLine("数据加载成功!"); Console.Read(); }
// Save all values from config file public void Save() { if (MySandboxGame.IsDedicated) { return; } MySandboxGame.Log.WriteLine("MyConfig.Save() - START"); MySandboxGame.Log.IncreaseIndent(); ProfilerShort.Begin("MyConfig.Save"); try { MySandboxGame.Log.WriteLine("Path: " + m_path, LoggingOptions.CONFIG_ACCESS); try { using (var stream = MyFileSystem.OpenWrite(m_path)) { XmlWriterSettings settings = new XmlWriterSettings() { Indent = true, NewLineHandling = NewLineHandling.None }; using (XmlWriter xmlWriter = XmlWriter.Create(stream, settings)) { XmlSerializer xmlSerializer = new XmlSerializer(m_values.GetType(), new Type[] { typeof(SerializableDictionary <string, string>), typeof(List <string>), typeof(SerializableDictionary <string, MyConfig.MyDebugInputData>), typeof(MyConfig.MyDebugInputData) }); xmlSerializer.Serialize(xmlWriter, m_values); } } } catch (Exception exc) { // Write exception to log, but continue as if nothing wrong happened MySandboxGame.Log.WriteLine("Exception occured, but application is continuing. Exception: " + exc); } } finally { ProfilerShort.End(); MySandboxGame.Log.DecreaseIndent(); MySandboxGame.Log.WriteLine("MyConfig.Save() - END"); } }
public MainWindow() { InitializeComponent(); // read persisted queries from file if (File.Exists(_fileName)) { var serializer = new XmlSerializer(persistedQueries.GetType()); var reader = new StreamReader(_fileName); persistedQueries = (SerializableDictionary <int, string>)serializer.Deserialize(reader); reader.Close(); foreach (var s in persistedQueries) { PeristedQList.Add(s.Value); } } // display the query list QueryDataRow.DataContext = PeristedQList; QueryDataRow.ItemsSource = PeristedQList; }
public List <int> GetShopListByUserID(int userID) { //模拟从MemCache中读取索引 SerializableDictionary <int, List <int> > dic = new SerializableDictionary <int, List <int> >(); byte[] bytes = Encoding.UTF8.GetBytes(File.ReadAllText("F://1.txt", Encoding.UTF8)); var memoryStream = new MemoryStream(); memoryStream.Write(bytes, 0, bytes.Count()); memoryStream.Seek(0, SeekOrigin.Begin); XmlSerializer xml = new XmlSerializer(dic.GetType()); var obj = xml.Deserialize(memoryStream) as Dictionary <int, List <int> >; return(obj[userID]); }
public List<int> GetShopListByUserID(int userID) { //模拟从MemCache中读取索引 SerializableDictionary<int, List<int>> dic = new SerializableDictionary<int, List<int>>(); byte[] bytes = Encoding.UTF8.GetBytes(File.ReadAllText("F://1.txt", Encoding.UTF8)); var memoryStream = new MemoryStream(); memoryStream.Write(bytes, 0, bytes.Count()); memoryStream.Seek(0, SeekOrigin.Begin); XmlSerializer xml = new XmlSerializer(dic.GetType()); var obj = xml.Deserialize(memoryStream) as Dictionary<int, List<int>>; return obj[userID]; }
static void Main(string[] args) { //模拟从数据库加载索引到内存中,形成内存中的数据库 //这里的 "Dictionary" 用来表达“一个用户注册过多少店铺“,即UserID与ShopID的一对多关系 SerializableDictionary<int, List<int>> dic = new SerializableDictionary<int, List<int>>(); List<int> shopIDList = new List<int>(); for (int shopID = 300000; shopID < 300050; shopID++) shopIDList.Add(shopID); int UserID = 15; //假设这里已经维护好了UserID与ShopID的关系 dic.Add(UserID, shopIDList); XmlSerializer xml = new XmlSerializer(dic.GetType()); var memoryStream = new MemoryStream(); xml.Serialize(memoryStream, dic); memoryStream.Seek(0, SeekOrigin.Begin); //将Dictionary持久化,相当于模拟保存在Mencache里面 File.AppendAllText("F://1.txt", Encoding.UTF8.GetString(memoryStream.ToArray())); Console.WriteLine("数据加载成功!"); Console.Read(); }
public void UnitTestSerialize() { SerializableDictionary<string, string> dictionary = new SerializableDictionary<string, string>(); dictionary.Add("myKey", "myValue"); XmlSerializer xmlSerializer = new XmlSerializer(dictionary.GetType()); string xml; using (MemoryStream memoryStream = new MemoryStream()) { using (XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8)) { using (StreamReader streamReader = new StreamReader(memoryStream, Encoding.UTF8)) { xmlSerializer.Serialize(xmlTextWriter, dictionary); memoryStream.Position = 0; xml = streamReader.ReadToEnd(); streamReader.Close(); } } } Assert.AreEqual("<?xml version=\"1.0\" encoding=\"utf-8\"?><Dictionary><item><key><string>myKey</string></key><value><string>myValue</string></value></item></Dictionary>", xml, "The XML representing the serialized dictionary was not as expected."); }
public SerializableDictionary <CpdInputControls, ControlBinding> LoadControlBindings(Mediator mediator) { var controlBindings = new SerializableDictionary <CpdInputControls, ControlBinding>(); foreach (CpdInputControls val in Enum.GetValues(typeof(CpdInputControls))) { controlBindings.Add(val, new ControlBinding()); } var bindings = Settings.Default.ControlBindings; if (!String.IsNullOrEmpty(bindings)) { var deserialized = Common.Serialization.Util.DeserializeFromXml(bindings, controlBindings.GetType()); if (deserialized != null) { var asBindings = (SerializableDictionary <CpdInputControls, ControlBinding>)deserialized; foreach (var entry in asBindings) { var thisEntry = entry.Value; if (thisEntry.BindingType == BindingType.DirectInputAxisBinding || thisEntry.BindingType == BindingType.DirectInputButtonBinding || thisEntry.BindingType == BindingType.DirectInputPovBinding) { if (mediator.DeviceMonitors.ContainsKey(thisEntry.DirectInputDevice.Guid)) { var thisBinding = controlBindings[entry.Key]; thisBinding.BindingType = thisEntry.BindingType; thisBinding.ControlName = thisEntry.ControlName; thisBinding.Keys = thisEntry.Keys; thisBinding.PovDirection = thisEntry.PovDirection; thisBinding.CpdInputControl = thisEntry.CpdInputControl; thisBinding.DirectInputDevice = mediator.DeviceMonitors[thisEntry.DirectInputDevice.Guid].DeviceInfo; foreach (var control in thisBinding.DirectInputDevice.Controls) { if (control.ControlNum == thisEntry.DirectInputControl.ControlNum && control.ControlType == thisEntry.DirectInputControl.ControlType) { thisBinding.DirectInputControl = (DIPhysicalControlInfo)control; break; } } } } else if (thisEntry.BindingType == BindingType.Keybinding) { var thisBinding = controlBindings[entry.Key]; thisBinding.CpdInputControl = thisEntry.CpdInputControl; thisBinding.BindingType = thisEntry.BindingType; thisBinding.ControlName = thisEntry.ControlName; thisBinding.Keys = thisEntry.Keys; } } } } return(controlBindings); }
public void ShouldIgnorePrivateClassExtension() { var dictionary = new SerializableDictionary<string, object>(); dictionary.Add("foo", new PrivateClass()); var serializer = new XmlSerializer(dictionary.GetType()); using (var writer = XmlWriter.Create(Console.Out, new XmlWriterSettings { Indent = true })) { serializer.Serialize(writer, dictionary); } var mem = new MemoryStream(); serializer.Serialize(mem, dictionary); mem.Position = 0; var deserialized = (Dictionary<string, object>)serializer.Deserialize(mem); Assert.AreEqual(0, deserialized.Count); }
public void ShouldRoundtripXml() { var dictionary = new SerializableDictionary<string, object>(); dictionary.Add("foo", new Foo { Name = "name", Bar = new Bar { Value = "value", }, }); var serializer = new XmlSerializer(dictionary.GetType()); using (var writer = XmlWriter.Create(Console.Out, new XmlWriterSettings { Indent = true })) { serializer.Serialize(writer, dictionary); } var mem = new MemoryStream(); serializer.Serialize(mem, dictionary); mem.Position = 0; var deserialized = (Dictionary<string, object>)serializer.Deserialize(mem); Assert.AreEqual(dictionary.Count, deserialized.Count); }