public string ToXMLString() { XmlDocument playerData = new XmlDocument(); // create top-level node XmlNode player = playerData.CreateElement("Player"); playerData.AppendChild(player); // create stats node XmlNode stats = playerData.CreateElement("Stats"); player.AppendChild(stats); XmlNode currentHitPoints = playerData.CreateElement("CurrentHitPoints"); currentHitPoints.AppendChild(playerData.CreateTextNode(CurrentHitPoints.ToString())); stats.AppendChild(currentHitPoints); XmlNode maximumHitPoints = playerData.CreateElement("MaximumHitPoints"); maximumHitPoints.AppendChild(playerData.CreateTextNode(MaximumHitPoints.ToString())); stats.AppendChild(maximumHitPoints); XmlNode gold = playerData.CreateElement("Gold"); gold.AppendChild(playerData.CreateTextNode(Gold.ToString())); stats.AppendChild(gold); XmlNode experiencePoints = playerData.CreateElement("ExperiencePoints"); experiencePoints.AppendChild(playerData.CreateTextNode(Gold.ToString())); stats.AppendChild(experiencePoints); XmlNode playerLevel = playerData.CreateElement("Level"); playerLevel.AppendChild(playerData.CreateTextNode(Level.ToString())); stats.AppendChild(playerLevel); XmlNode currentLocation = playerData.CreateElement("CurrentLocation"); currentLocation.AppendChild(playerData.CreateTextNode(CurrentLocation.ID.ToString())); stats.AppendChild(currentLocation); if (CurrentWeapon != null) { XmlNode currentWeapon = playerData.CreateElement("CurrentWeapon"); currentWeapon.AppendChild(playerData.CreateTextNode(CurrentWeapon.ID.ToString())); stats.AppendChild(currentWeapon); } // create inventory node XmlNode inventoryItems = playerData.CreateElement("InventoryItems"); player.AppendChild(inventoryItems); foreach (InventoryItem item in Inventory) { XmlNode inventoryItem = playerData.CreateElement("InventoryItem"); XmlAttribute idAttribute = playerData.CreateAttribute("ID"); idAttribute.Value = item.Details.ID.ToString(); inventoryItem.Attributes.Append(idAttribute); XmlAttribute quantityAttribute = playerData.CreateAttribute("Quantity"); quantityAttribute.Value = item.Quantity.ToString(); inventoryItem.Attributes.Append(quantityAttribute); inventoryItems.AppendChild(inventoryItem); } // create player's quests node XmlNode playerQuests = playerData.CreateElement("PlayerQuests"); player.AppendChild(playerQuests); foreach (PlayerQuest quest in Quests) { XmlNode playerQuest = playerData.CreateElement("PlayerQuest"); XmlAttribute idAttribute = playerData.CreateAttribute("ID"); idAttribute.Value = quest.Details.ID.ToString(); playerQuest.Attributes.Append(idAttribute); XmlAttribute isCompletedAttibute = playerData.CreateAttribute("IsCompleted"); isCompletedAttibute.Value = quest.IsCompleted.ToString(); playerQuest.Attributes.Append(isCompletedAttibute); playerQuests.AppendChild(playerQuest); } return(playerData.InnerXml); }
public string ToXmlString() { XmlDocument playerData = new XmlDocument(); //Create the top-level node XmlNode player = playerData.CreateElement("Player"); playerData.AppendChild(player); //Create the "Stats" child node to hold the other player stats nodes XmlNode stats = playerData.CreateElement("Stats"); player.AppendChild(stats); //Create the child nodes for the "Stats" nodes XmlNode currentHitPoints = playerData.CreateElement("CurrentHitPoints"); currentHitPoints.AppendChild(playerData.CreateTextNode(CurrentHitPoints.ToString())); stats.AppendChild(currentHitPoints); XmlNode maximumHitPoints = playerData.CreateElement("MaximumHitPoints"); maximumHitPoints.AppendChild(playerData.CreateTextNode(MaximumHitPoints.ToString())); stats.AppendChild(maximumHitPoints); XmlNode gold = playerData.CreateElement("Gold"); gold.AppendChild(playerData.CreateTextNode(Gold.ToString())); stats.AppendChild(gold); XmlNode experiencePoints = playerData.CreateElement("ExperiencePoints"); experiencePoints.AppendChild(playerData.CreateTextNode(ExperiencePoints.ToString())); stats.AppendChild(experiencePoints); XmlNode currentLocation = playerData.CreateElement("CurrentLocation"); currentLocation.AppendChild(playerData.CreateTextNode(CurrentLocation.ID.ToString())); stats.AppendChild(currentLocation); if (CurrentWeapon != null) { XmlNode currentWeapon = playerData.CreateElement("CurrentWeapon"); currentWeapon.AppendChild(playerData.CreateTextNode(CurrentWeapon.ID.ToString())); stats.AppendChild(currentWeapon); } //Create the "InventoryItems" child node to hold each InventoryItem node XmlNode inventoryItems = playerData.CreateElement("InventoryItems"); player.AppendChild(inventoryItems); //Create an "InventoryItem" node for each item in the player inventory foreach (InventoryItem item in this.Inventory) { XmlNode inventoryItem = playerData.CreateElement("InventoryItem"); XmlAttribute idAttribute = playerData.CreateAttribute("ID"); idAttribute.Value = item.Details.ID.ToString(); inventoryItem.Attributes.Append(idAttribute); XmlAttribute quantityAttribute = playerData.CreateAttribute("Quantity"); quantityAttribute.Value = item.Quantity.ToString(); inventoryItem.Attributes.Append(quantityAttribute); inventoryItems.AppendChild(inventoryItem); } //Create the "PlayerQuests" child node to hold each PlayerQuest node XmlNode playerQuests = playerData.CreateElement("PlayerQuests"); player.AppendChild(playerQuests); //Create a "PlayerQuest" node for each quest the player has acquired foreach (PlayerQuest quest in this.Quests) { XmlNode playerQuest = playerData.CreateElement("PlayerQuest"); XmlAttribute idAttribute = playerData.CreateAttribute("ID"); idAttribute.Value = quest.Details.ID.ToString(); playerQuest.Attributes.Append(idAttribute); XmlAttribute isCompletedAttribute = playerData.CreateAttribute("IsCompleted"); isCompletedAttribute.Value = quest.IsCompleted.ToString(); playerQuest.Attributes.Append(isCompletedAttribute); playerQuests.AppendChild(playerQuest); } return(playerData.InnerXml); //The XML document, as a string, so we can save the data to disk }
public string ToXmlString() { XmlDocument saveData = new XmlDocument(); // Create the top-level XML node XmlNode player = saveData.CreateElement("Player"); saveData.AppendChild(player); // Create the "Stats" child node to hold the other player statistics nodes XmlNode stats = saveData.CreateElement("Stats"); player.AppendChild(stats); // Create the child nodes for the "Stats" node XmlNode currentHitPoints = saveData.CreateElement("CurrentHitPoints"); currentHitPoints.AppendChild(saveData.CreateTextNode(CurrentHitPoints.ToString())); stats.AppendChild(currentHitPoints); XmlNode maximumHitPoints = saveData.CreateElement("MaximumHitPoints"); maximumHitPoints.AppendChild(saveData.CreateTextNode(MaximumHitPoints.ToString())); stats.AppendChild(maximumHitPoints); XmlNode gold = saveData.CreateElement("Gold"); gold.AppendChild(saveData.CreateTextNode(Gold.ToString())); stats.AppendChild(gold); XmlNode homeTile = saveData.CreateElement("HomeTile"); homeTile.AppendChild(saveData.CreateTextNode(HomeTileID.ToString())); stats.AppendChild(homeTile); XmlNode currentTile = saveData.CreateElement("CurrentTile"); currentTile.AppendChild(saveData.CreateTextNode(CurrentTile.ID.ToString())); stats.AppendChild(currentTile); // Create the "PlayerSkills" child node to hold each PlayerSkill node XmlNode playerSkills = saveData.CreateElement("PlayerSkills"); player.AppendChild(playerSkills); // Create an "PlayerSkill" node for each skill foreach (PlayerSkill skill in Skills) { XmlNode playerSkill = saveData.CreateElement("PlayerSkill"); XmlAttribute typeAttribute = saveData.CreateAttribute("Type"); typeAttribute.Value = skill.ID.ToString(); playerSkill.Attributes.Append(typeAttribute); XmlAttribute levelAttribute = saveData.CreateAttribute("Level"); levelAttribute.Value = skill.Level.ToString(); playerSkill.Attributes.Append(levelAttribute); XmlAttribute experienceAttribute = saveData.CreateAttribute("Experience"); experienceAttribute.Value = skill.Experience.ToString(); playerSkill.Attributes.Append(experienceAttribute); playerSkills.AppendChild(playerSkill); } // Create the "InventoryItems" child node to hold each InventoryItem node XmlNode inventoryItems = saveData.CreateElement("InventoryItems"); player.AppendChild(inventoryItems); // Create an "InventoryItem" node for each item in the player's inventory foreach (InventoryItem item in Inventory) { XmlNode inventoryItem = saveData.CreateElement("InventoryItem"); XmlAttribute idAttribute = saveData.CreateAttribute("ID"); idAttribute.Value = item.Data.ID.ToString(); inventoryItem.Attributes.Append(idAttribute); XmlAttribute quantityAttribute = saveData.CreateAttribute("Quantity"); quantityAttribute.Value = item.Quantity.ToString(); inventoryItem.Attributes.Append(quantityAttribute); inventoryItems.AppendChild(inventoryItem); } // Create the "PlayerQuests" child node to hold each PlayerQuest node XmlNode playerQuests = saveData.CreateElement("PlayerQuests"); player.AppendChild(playerQuests); // Create a "PlayerQuest" node for each quest the player has acquired foreach (PlayerQuest quest in Quests) { XmlNode playerQuest = saveData.CreateElement("PlayerQuest"); XmlAttribute idAttribute = saveData.CreateAttribute("ID"); idAttribute.Value = quest.Data.ID.ToString(); playerQuest.Attributes.Append(idAttribute); XmlAttribute stateAttribute = saveData.CreateAttribute("State"); stateAttribute.Value = quest.State.ToString(); playerQuest.Attributes.Append(stateAttribute); XmlAttribute isCompletedAttribute = saveData.CreateAttribute("IsComplete"); isCompletedAttribute.Value = quest.IsComplete.ToString(); playerQuest.Attributes.Append(isCompletedAttribute); playerQuests.AppendChild(playerQuest); } // Create the "ItemEquipments" child node to hold each ItemEquipment node XmlNode itemEquipments = saveData.CreateElement("ItemEquipments"); player.AppendChild(itemEquipments); // Create an "ItemEquipment" node for each equipped item foreach (ItemEquipment item in Equipment) { XmlNode itemEquipment = saveData.CreateElement("ItemEquipment"); XmlAttribute idAttribute = saveData.CreateAttribute("ID"); idAttribute.Value = item.ID.ToString(); itemEquipment.Attributes.Append(idAttribute); itemEquipments.AppendChild(itemEquipment); } return(saveData.InnerXml); }