简体   繁体   English

经常更新xml文件的最佳方法是什么?

[英]What is the best way to update an xml file often?

I work on a small 3D voyel game like minecraft style. 我从事类似于Minecraft风格的小型3D航海游戏。 In my game you can remove and add some cube. 在我的游戏中,您可以删除并添加一些立方体。 Because of that i always need update/save my XML file. 因此,我总是需要更新/保存我的XML文件。 All the world is save in a xml file . 全世界都保存在xml文件中。 Everything work about add/remove node in my xml file , but the problem is about the performance ! 一切都与我的xml文件中的添加/删除节点有关,但是问题在于性能!

Each time i need save in the xml file , the program need sleep for 1 seconde . 每次我需要保存在xml文件中时,程序需要休​​眠1秒。 This is really ugly in my game .. each time the player add a cube mean wait 1 seconde for save into my xml file. 这在我的游戏中确实很丑..每次玩家添加一个立方体意味着要等待1秒保存到我的xml文件中。

Do you have some idea ? 你有什么主意吗?

Here is the way i save my xml file 这是我保存xml文件的方式

Example (after remove a cube) 示例(删除多维数据集之后)

            XElement elementToRemove = elements
              .FirstOrDefault(
              x => (string)x.Attribute("min") == buildMin.ToString() &&
                    (string)x.Attribute("max") == buildMax.ToString());

            if(elementToRemove != null)
            {
                elementToRemove.Remove();
            }
            //Xdocument Class ~ call this function need 1 seconde
            XDocumentWorld.Save(AppDomain.CurrentDomain.BaseDirectory + @"World\world.xml");

Honestly, you shouldn't be saving your XML every update. 老实说,您不应该在每次更新时都保存XML。 This is classed as bad code itself and therefore I wouldn't advise on using it. 这本身被归为错误代码,因此我不建议您使用它。 You should rather, instead, keep the information in memory and write on exit. 相反,您应该将信息保留在内存中并在退出时写入。

However, if you insist, you could in theory use another thread and schedule the code to write to the XML file every say 5 seconds reducing the load on the main thread and still keeping the functionality that you are requesting. 但是,如果您坚持认为,从理论上讲,您可以使用另一个线程,并安排代码每隔5秒写入XML文件一次,从而减少了主线程的负担,并仍然保留了您所请求的功能。

Have a look at the link below if you're unsure on how to use threads: 如果不确定如何使用线程,请查看下面的链接:

http://www.albahari.com/threading/ http://www.albahari.com/threading/

You don't have to save world state after each edit. 您无需在每次编辑后保存世界状态。 Save the data before exiting the game or when a chunk (ie, a region) is going to be removed from memory. 在退出游戏之前或要从内存中删除一个块(即一个区域)之前,请保存数据。 Also, you can store just the list of player actions instead of all the blocks. 另外,您可以仅存储玩家动作列表,而不是所有块。 To resume you regenerate everything using the same seed (assuming a random world) and then redo those actions. 要恢复,请使用相同的种子(假设为随机世界)重新生成所有内容,然后重做这些操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM