示例#1
0
文件: Form1.cs 项目: bangush/csharp
        private void LoadSpeeds()
        {
            string path = Application.StartupPath;
            string file = path + "\\speeds.xml";

            if (File.Exists(file))
            {
                System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(this.si.GetType());
                FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
                try
                {
                    si = xs.Deserialize(fs) as SpeedInfo;
                }catch {}
                fs.Close();
            }

            /*
             * if(File.Exists(file))
             * {
             *  XmlDocument xd = new XmlDocument();
             *  xd.Load(file);
             *
             *  foreach (XmlNode xn in xd.DocumentElement.SelectNodes("//speedinfo/speeds/speed"))
             *  {
             *      Speed sp = new Speed();
             *      foreach (PropertyInfo pi in sp.GetType().GetProperties().Where(x => x.CanWrite))
             *      {
             *          var el = xn.Attributes[pi.Name];
             *          if (el != null)
             *          {
             *              object val = el.Value;
             *              if (pi.PropertyType == typeof(DateTime))
             *              {
             *                  val = DateTime.Parse(val.ToString());
             *              }
             *              else if (pi.PropertyType == typeof(double))
             *              {
             *                  val = double.Parse(val.ToString());
             *              }
             *              else if (pi.PropertyType == typeof(int))
             *              {
             *                  val = int.Parse(val.ToString());
             *              }
             *              else if (pi.PropertyType == typeof(Int32))
             *              {
             *                  val = Int32.Parse(val.ToString());
             *              }
             *              else if (pi.PropertyType == typeof(Int64))
             *              {
             *                  val = Int64.Parse(val.ToString());
             *              }
             *              else if (pi.PropertyType == typeof(Direction))
             *              {
             *                  val = val.Equals("Down") ? Direction.Down : val.Equals("Up") ? Direction.Up : Direction.UnKnown;
             *              }
             *              else if (pi.PropertyType == typeof(string))
             *              {
             *                  val = val.ToString();
             *              }
             *              pi.SetValue(sp, val, null);
             *          }
             *      }
             *      si.Add(sp);
             *  }
             * }
             */
        }
示例#2
0
文件: Form1.cs 项目: Hagser/csharp
        private void LoadSpeeds()
        {
            string path = Application.StartupPath;
            string file = path + "\\speeds.xml";
            if (File.Exists(file))
            {
                System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(this.si.GetType());
                FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
                try
                {
                    si = xs.Deserialize(fs) as SpeedInfo;
                }catch{}
                fs.Close();
            }
            /*
            if(File.Exists(file))
            {
                XmlDocument xd = new XmlDocument();
                xd.Load(file);

                foreach (XmlNode xn in xd.DocumentElement.SelectNodes("//speedinfo/speeds/speed"))
                { 
                    Speed sp = new Speed();
                    foreach (PropertyInfo pi in sp.GetType().GetProperties().Where(x => x.CanWrite))
                    {
                        var el = xn.Attributes[pi.Name];
                        if (el != null)
                        {
                            object val = el.Value;
                            if (pi.PropertyType == typeof(DateTime))
                            {
                                val = DateTime.Parse(val.ToString());
                            }
                            else if (pi.PropertyType == typeof(double))
                            {
                                val = double.Parse(val.ToString());
                            }
                            else if (pi.PropertyType == typeof(int))
                            {
                                val = int.Parse(val.ToString());
                            }
                            else if (pi.PropertyType == typeof(Int32))
                            {
                                val = Int32.Parse(val.ToString());
                            }
                            else if (pi.PropertyType == typeof(Int64))
                            {
                                val = Int64.Parse(val.ToString());
                            }
                            else if (pi.PropertyType == typeof(Direction))
                            {
                                val = val.Equals("Down") ? Direction.Down : val.Equals("Up") ? Direction.Up : Direction.UnKnown;
                            }
                            else if (pi.PropertyType == typeof(string))
                            {
                                val = val.ToString();
                            }
                            pi.SetValue(sp, val, null);
                        }
                    }
                    si.Add(sp);
                }
            }
            */
        }