public Form1()
        {
            InitializeComponent();

            KmlDocument kdoc = new KmlDocument();
            kdoc.LoadFromFile("Test.kml");

            KmlNodeCollection nodeCollection = kdoc.SelectNodes("item");
            int i = 0;
            int errors = 0;
            DateTime start = DateTime.Now;
            foreach (KmlNode knode in nodeCollection)
            {
                i++;
                try
                {
                    textBox1.AppendText(knode.SelectSingleNode("Index").Values[1] + "\r\n");
                    //textBox1.AppendText(knode.Values[1] + "\n");
                    //textBox1.AppendText(knode.ToString() + "\r\n");
                    // MessageBox.Show(knode.ToString());
                }
                catch (Exception)
                {
                    errors++;
                }
            }
               // textBox1.Text = kdoc.ToString();
            DateTime end = DateTime.Now;
            TimeSpan span = end - start;
            MessageBox.Show("Read and re-generated " + i + " items with " + errors + " errors in " + span.TotalSeconds + " seconds.");
            //MessageBox.Show(kdoc.RootNode.ToString());
        }
        private void UpdateItems()
        {
            KmlDocument kdoc = new KmlDocument();
            kdoc.LoadFromFile(MessageE);
            kdoc.LoadFromFile(InitItem);

            ItemNames = new Dictionary<int, string>();
            ItemDescriptions = new Dictionary<int, string>();

            //load item names
            foreach (KmlNode knode in kdoc.SelectNodes("itemname"))
            {
                int Index = knode.Values[1].ValueAsInt;
                string Name = knode.Values[2].Value;

                if (!ItemNames.ContainsKey(Index))
                {
                    ItemNames.Add(Index, Name);
                }
            }

            //load prefix names
            using (ISession session = Database.Factory.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    foreach (KmlNode knode in kdoc.SelectNodes("prefixname"))
                    {
                        int Index = knode.Values[1].ValueAsInt;
                        string Name = knode.Values[2].Value;
                        Prefix prefix = new Prefix();
                        prefix.Index = Index;
                        prefix.Name = Name;
                        session.SaveOrUpdate(prefix);
                    }
                    transaction.Commit();
                }
            }
            //load item descriptions
            foreach (KmlNode knode in kdoc.SelectNodes("itemdesc"))
            {
                int Index = knode.Values[1].ValueAsInt;
                string Name = knode.Values[2].Value;

                if (!ItemDescriptions.ContainsKey(Index))
                {
                    ItemDescriptions.Add(Index, Name);
                }
            }

            //delete current items

            //load items

            using (ISession session = Database.Factory.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {

                    foreach (KmlNode knode in kdoc.SelectNodes("item"))
                    {

                        int Index = knode.SelectSingleNode("index").Values[1].ValueAsInt;
                        int NameIndex = knode.SelectSingleNode("name").Values[1].ValueAsInt;

                        //description
                        int DescIndex = -1;
                        if (knode.ContainsNode("desc"))
                        {
                            DescIndex = knode.SelectSingleNode("desc").Values[1].ValueAsInt;
                        }

                        string Icon = knode.SelectSingleNode("image").Values[1].Value;
                        string Class = knode.SelectSingleNode("class").Values[1].Value;
                        string SubClass = knode.SelectSingleNode("class").Values[2].Value;
                        int Code1 = knode.SelectSingleNode("code").Values[1].ValueAsInt;
                        int Code2 = knode.SelectSingleNode("code").Values[2].ValueAsInt;
                        int Code3 = knode.SelectSingleNode("code").Values[3].ValueAsInt;
                        int Code4 = knode.SelectSingleNode("code").Values[4].ValueAsInt;

                        byte Endurance = 0;
                        if (knode.ContainsNode("endurance"))
                        {
                            Endurance = (byte)knode.SelectSingleNode("endurance").Values[1].ValueAsInt;
                        }

                        int Grade = 0;
                        if (knode.ContainsNode("level"))
                        {
                            Grade = knode.SelectSingleNode("level").Values[1].ValueAsInt;
                        }

                        //limit
                        int ClassLimit = -1;
                        int LevelLimit = -1;
                        if (knode.ContainsNode("limit"))
                        {

                            LevelLimit = knode.SelectSingleNode("limit").Values[2].ValueAsInt;

                            switch (knode.SelectSingleNode("limit").Values[2].Value.ToLower())
                            {
                                case "knight":
                                    ClassLimit = 0;
                                    break;
                                case "archer":
                                    ClassLimit = 2;
                                    break;
                                case "mage":
                                    ClassLimit = 1;
                                    break;
                            }
                        }

                        Item item = new Item();

                        item.Index = Index;
                        if (ItemNames.ContainsKey(NameIndex))
                        {
                            item.Name = ItemNames[NameIndex];
                        }
                        if (ItemDescriptions.ContainsKey(DescIndex))
                        {
                            item.Description = ItemDescriptions[DescIndex];
                        }

                        item.Icon = Icon;
                        item.Class = Class;
                        item.SubClass = SubClass;
                        item.Code1 = Code1;
                        item.Code2 = Code2;
                        item.Code3 = Code3;
                        item.Code4 = Code4;
                        item.Grade = Grade;
                        item.LevelLimit = LevelLimit;
                        item.ClassLimit = ClassLimit;
                        item.Endurance = Endurance;

                        session.SaveOrUpdate(item);

                    }

                    transaction.Commit();
                }

            }
        }