Question

I'm currently developing a sprite edition tool that by clicking on sprites in a spritesheet populates a DataGridView with the id, position and size of that sprite and then by pressing a button it export those rows to an XML file like this:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfModule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Module>
    <ID>0</ID>
    <posx>247</posx>
    <posy>87</posy>
    <width>36</width>
    <height>72</height>
  </Module>
  <Module>
    <ID>1</ID>
    <posx>197</posx>
    <posy>87</posy>
    <width>32</width>
    <height>70</height>
  </Module>
</ArrayOfModule>

I'm using a XmlSerializer for this. I serealize a list of type module with those 5 attributes. The thing is that i want to add a new tag that is not an attribute of the module class that represents the image used for the sprite sheet, so i can import those modules and the image at the same time. I want my XML something like this:

<?xml version="1.0" encoding="utf-8"?>
<Image>C:\sprite_sheet.png</Image>
<ArrayOfModule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Module>
    <ID>0</ID>
    <posx>247</posx>
    <posy>87</posy>
    <width>36</width>
    <height>72</height>
  </Module>
</ArrayOfModule>

How can i accomplish this? Can i do this with the XmlSerializer or do i need something else?

Was it helpful?

Solution

You'll have to create a wrapper class:

public class AllInfo
{
    public string Image {get;set;}
    public Module[] Modules {get;set;}
}

When you pass an instance of this class to the serializer, it will serialize both pieces of information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top