简体   繁体   中英

How can I save XML to my compiled XNB file in C# XNA?

Summarizing, how can I write data to my .xnb XML file which I read from before?


I've the following:

Editable XML file in my project's content folder:

<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
  <Asset Type="TestProgram_Data.Data">
    <Number>123</Number>
  </Asset>
</XnaContent>

My Data class:

namespace TestProgram_Data
{
    public class Data
    {
        public int Number;
    }
}

Now I can load data from a compiled XML file (.xnb, XNA content file) like this:

Data data = new Data();
Console.WriteLine("Number: " + data.Number); // Output: "Number: 123"

But how can I write to this file (.xnb file under Content folder)?

data.Number = 123456789; // Doesn't work

I tried to use XmlWriter with IntermediateSerializer, but it's made a normal XML file. I can make an .xnb file with this, too, but in this case the .xnb file isn't compiled (I can edit with Notepad, isn't part of my project, and if I run again my program, the default data [eg. 123] loads again - because I read from my .xnb file and not from the other file which created with XmlWriter).

At all, is possible to save data back to the .xnb file, from I read in the past?

It is not possible. XNB is a read-only format, designed for fast runtime loading of content. It is only written by the Content Pipeline at build time. You have to write the modified data to a new file in different format.

Additionally, IntermediateSerializer is part of the Content Pipeline, which is only included in XNA development tools. It is not included in the end-user redistributable. So you should not use that for creating the new file, but rather some other serializer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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