2013-12-07 30 views
-1

我目前正在開發一個精靈版本工具,通過點擊一個spritesheet中的精靈填充一個DataGridView與該精靈的id,位置和大小,然後按下按鈕它將這些行導出到這樣的XML文件中:如何添加到XML序列化一個類獨立標籤

<?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> 

我爲此使用了XmlSerializer。我用這5個屬性實現了一個類型模塊列表。問題是我想添加一個新的標籤,它不是表示用於精靈表的圖像的模塊類的屬性,所以我可以同時導入這些模塊和圖像。我想我的XML是這樣的:

<?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> 

我該如何做到這一點? 我可以用XmlSerializer來做這件事,還是我需要別的東西?

+0

我沒有看到您的新屬性或其他。 –

+0

標籤上方是新標籤 Xirion11

+0

我認爲您當前正在將'Module []'傳遞給XML序列化程序? –

回答

0

你必須創建一個包裝類:

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

當你通過這個類的一個實例串行化器,它會序列的兩條信息。

+0

這正是我所需要的。謝謝。 – Xirion11

相關問題