XML库存系统

时间:2014-01-22 11:01:33

标签: c# xml

我正在尝试测试我正在制作的库存系统,但我想用XML制作它。 输入代码等:

    <Item>
  <ItemName ItemName = "Apple">
    <ItemQuantity>1</ItemQuantity>
    <ItemMaxQuantity>64</ItemMaxQuantity>
    <ItemState>PERFECT</ItemState>
  </ItemName>

  <ItemName ItemName = "Sword">
    <ItemQuantity>1</ItemQuantity>
    <ItemMaxQuantity>1</ItemMaxQuantity>
    <ItemState>BROKEN</ItemState>
  </ItemName>
</Item>

我知道如何序列化等等。但是不知道如何进一步将它们放入游戏中,拥有像我所展示的多个项目。 我有一个库存类。 就是这样:

namespace inventory_test
{
    class Inventory
    {
        public enum ItemState
        {
            NONE,
            PERFECT,
            FINE,
            BROKEN,
            DESTROYED
        };

        /*public enum ItemType
        {
            FOOD,
            USEABLE,
            WEAPON,
            KEY,
            TOOL
        }; Implement this in the test later*/

        [XmlElement("ItemName")]
        public string ItemName
        { get; set; }

        [XmlElement("ItemState")]
        public string ItemState
        { get; set; }

        [XmlElement("ItemQuantity")]
        public string Quantity
        { get; set; }

        [XmlElement("ItemMaxQuantity")]
        public string MaxQuantity
        { get; set; }

        static void AddItem(int itemID, string itemState, int quantity, int maxquantity)
        {

        }


    }
}

这是我的program.cs:

    namespace inventory_test
{
    class Program
    {
        static void Main(string[] args)
        {
        }
//Serialization
        static public void Serialize(Inventory inventory)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Inventory));
            using (TextWriter writer = new StreamWriter(@"C:\Xml.xml"))
            {
                serializer.Serialize(writer, inventory);
            }
        }
    }
}

我不知道我现在要做什么。 我现在真的被困住了,不知道该怎么办。 帮帮我:))

2 个答案:

答案 0 :(得分:1)

此示例代码显示如何将List of Inventory序列化为xml文件,以及如何读取文件并生成相同的Inventory列表。

var serializer = new XmlSerializer(typeof(List<Inventory>));
var inventories = new List<Inventory>();
inventories.Add(new Inventory
{
    ItemName = "Apple",
    Quantity = "1",
    MaxQuantity = "64",
    ItemState = "PERFECT"
});
inventories.Add(new Inventory
{
    ItemName = "Sword",
    Quantity = "1",
    MaxQuantity = "1",
    ItemState = "BROKEN"
});
//write to xml file
using (var writer = new StreamWriter("inventory.xml"))
{
    serializer.Serialize(writer, inventories);
}
//read from xml file and generate List<Inventory>
using (var reader = new StreamReader("inventory.xml"))
{
    //variable 'result' will contain the same value as 'inventories'
    var result = (List<Inventory>)serializer.Deserialize(reader);
}

答案 1 :(得分:0)

根据你在我之前的回答中的评论,意味着你不想使用序列化/反序列化方法,但“手动”方法并不是这样。您可以使用XDocument和linq-to-xml查询“手动”编写和读取xml文件。这是一个例子:

//writing to xml file
var doc = new XDocument(new XDeclaration("1.0", "utf-8", "no"));
var root = new XElement("Item");
doc.Add(root);
foreach (var inventory in inventories)
{
    var itemNode = new XElement("ItemName");
    var itemAttribute = new XAttribute("ItemName", inventory.ItemName);
    itemNode.Add(itemAttribute);
    var itemQty = new XElement("ItemQuantity", inventory.Quantity);
    itemNode.Add(itemQty);
    var itemMaxQty = new XElement("ItemMaxQuantity", inventory.MaxQuantity);
    itemNode.Add(itemMaxQty);
    var itemState = new XElement("ItemState", inventory.ItemState);
    itemNode.Add(itemState);
    root.Add(itemNode);
}
doc.Save("Inventory.xml");

//reading from xml file created by above codes to create List<Inventory> object
var docFromFile = XDocument.Load("Inventory.xml");
var inventoriesFromFile = (from inventory in docFromFile.Descendants("ItemName")
                       select new Inventory
                                  {
                                      ItemName = inventory.Attribute("ItemName").Value,
                                      Quantity = inventory.Element("ItemQuantity").Value,
                                      MaxQuantity = inventory.Element("ItemMaxQuantity").Value,
                                      ItemState = inventory.Element("ItemState").Value
                                  }).ToList();

//in the end you'll get variable inventories and inventoriesFromFile have same value

请注意,上面代码中的inventories变量与我之前的答案(定义和内容/值)中的变量相同。同样在问题中,您只在XmlSerializer上展示了一些知识/努力而不是XDocument内容,因此请仔细阅读代码。如果你希望我在这个答案中解释每一行代码,那么写的东西太多了。祝你好运