0
votes

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?

1
Those are really only intended for fast readonly access to XML in the pipeline e.g. level design specifications. You don't change them at runtime. If you wish to do something like that, then use the standard Xml Serialization/Deserialization classes in C#.ManoDestra

1 Answers

1
votes

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.