Пример #1
0
        private void loadData()
        {
            XmlTextReader reader = new XmlTextReader(contentPath + "\\Consumables.item");
            reader.WhitespaceHandling = WhitespaceHandling.None;

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "Consumable")
                        consumable.name = reader["Name"];
                    if (reader.Name == "Image")
                        consumable.textureName = contentPath + "\\" + reader["Texture"];
                    if (reader.Name == "HP")
                        consumable.HP = int.Parse(reader["Value"]);
                    if (reader.Name == "MP")
                        consumable.MP = int.Parse(reader["Value"]);
                    if (reader.Name == "RegenHP")
                        consumable.regenHP = int.Parse(reader["Value"]);
                    if (reader.Name == "RegenMP")
                        consumable.regenMP = int.Parse(reader["Value"]);
                    if (reader.Name == "Resurrect")
                        consumable.Resurrect = reader["Value"];
                    if (reader.Name == "Life")
                        consumable.life = int.Parse(reader["Value"]);
                }

                else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "Consumable")
                {
                    consumableList.Add(consumable.name, consumable);
                    existingConsumableComboBox.Items.Add(consumable.name);
                    consumable = new Consumable();
                }
            }
            reader.Close();
        }
Пример #2
0
        private void saveButton_Click(object sender, EventArgs e)
        {
            if (HPTextBox.Text.Length < 1 || MPTextBox.Text.Length < 1 ||
            HPRegenTextBox.Text.Length < 1 || MPRegenTextBox.Text.Length < 1 || lifeTextBox.Text.Length < 1 ||
            imageFileTextBox.Text.Length < contentPath.Length + 1)
            {
                MessageBox.Show("Not All Fields Have Values");
                return;
            }

            consumable.name = nameTextBox.Text;
            consumable.textureName = imageFileTextBox.Text;
            consumable.HP = int.Parse(HPTextBox.Text);
            consumable.MP = int.Parse(MPTextBox.Text);
            consumable.regenHP = int.Parse(HPRegenTextBox.Text);
            consumable.regenMP = int.Parse(MPRegenTextBox.Text);
            consumable.Resurrect = reviveComboBox.Text;
            consumable.life = int.Parse(lifeTextBox.Text);

            if (existingConsumableComboBox.SelectedItem == null)
            {
                consumableList.Add(consumable.name, consumable);
                existingConsumableComboBox.Items.Add(consumable.name);
                existingConsumableComboBox.SelectedItem = consumable.name;
                consumable = new Consumable();
            }
            else
            {
                if (existingConsumableComboBox.Text != consumable.name)
                {
                    consumableList.Add(consumable.name, consumable);
                    consumableList.Remove(existingConsumableComboBox.Text);
                    existingConsumableComboBox.Items.Remove(existingConsumableComboBox.SelectedItem);
                    existingConsumableComboBox.Items.Add(consumable.name);
                    existingConsumableComboBox.SelectedItem = consumable.name;
                }

                else
                    consumableList[consumable.name] = consumable;

                consumable = new Consumable();
            }

            XmlDocument doc = new XmlDocument();

            XmlElement rootElement = doc.CreateElement("Consumables");
            doc.AppendChild(rootElement);

            foreach (Consumable con in consumableList.Values)
            {
                XmlElement consumableElement = doc.CreateElement("Consumable");
                rootElement.AppendChild(consumableElement);
                XmlAttribute wAttr = doc.CreateAttribute("Name");
                wAttr.Value = con.name;
                consumableElement.Attributes.Append(wAttr);
                XmlElement textureElement = doc.CreateElement("Image");
                consumableElement.AppendChild(textureElement);
                XmlAttribute tAttr = doc.CreateAttribute("Texture");
                tAttr.Value = con.textureName.Replace(contentPath + "\\", "");
                textureElement.Attributes.Append(tAttr);
                XmlElement HPElement = doc.CreateElement("HP");
                consumableElement.AppendChild(HPElement);
                XmlAttribute HPAttr = doc.CreateAttribute("Value");
                HPAttr.Value = con.HP.ToString();
                HPElement.Attributes.Append(HPAttr);
                XmlElement MPElement = doc.CreateElement("MP");
                consumableElement.AppendChild(MPElement);
                XmlAttribute MPAttr = doc.CreateAttribute("Value");
                MPAttr.Value = con.MP.ToString();
                MPElement.Attributes.Append(MPAttr);
                XmlElement RegenHPElement = doc.CreateElement("RegenHP");
                consumableElement.AppendChild(RegenHPElement);
                XmlAttribute RegenHPAttr = doc.CreateAttribute("Value");
                RegenHPAttr.Value = con.regenHP.ToString();
                RegenHPElement.Attributes.Append(RegenHPAttr);
                XmlElement RegenMPElement = doc.CreateElement("RegenMP");
                consumableElement.AppendChild(RegenMPElement);
                XmlAttribute RegenMPAttr = doc.CreateAttribute("Value");
                RegenMPAttr.Value = con.regenMP.ToString();
                RegenMPElement.Attributes.Append(RegenMPAttr);
                XmlElement ResurrectElement = doc.CreateElement("Resurrect");
                consumableElement.AppendChild(ResurrectElement);
                XmlAttribute ResAttr = doc.CreateAttribute("Value");
                ResAttr.Value = con.Resurrect;
                ResurrectElement.Attributes.Append(ResAttr);
                XmlElement lifeElement = doc.CreateElement("Life");
                consumableElement.AppendChild(lifeElement);
                XmlAttribute lifeAttr = doc.CreateAttribute("Value");
                lifeAttr.Value = con.life.ToString();
                lifeElement.Attributes.Append(lifeAttr);
            }
            doc.Save(contentPath + "\\" + "Consumables.item");
        }