我試圖理解序列化如何工作,但我一直在一個問題上停留了一段時間。無法序列化對象列表(每個對象包含一個列表)到xml
我試圖做的是我有一個處理對象列表的管理器。在這種情況下,食譜。在每個配方中都有一份配料清單。我的經理來自Listmanager類。成分列表還使用List manager類。
當我序列化一個類似的管理器,但我序列化爲二進制文件(.dat)時,我已經使用了這個。這工作,甚至與反序列化工作。我知道XML是一個有點不同,但還是值得一提..
的問題 - >我得到的是這樣的,當我序列:
<?xml version="1.0"?>
<RecipeManager xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
我想的XML文檔類似於像這樣的:
<Recipe>
<Name>Food<Name>
<Ingredient>blabla<Ingredient>
<Ingredient>blabla2<Ingredient>
<Ingredient>blabla3<Ingredient>
</Recipe>
這是我Listmanager,它還包含了許多其他的方法,但這只是讓你可以得到它的外觀的想法:
[Serializable]
public class ListManager<T> : IListManager<T>
{
private List<T> list;
public ListManager()
{
list = new List<T>();
}
public int Count { get { return list.Count; } }
public void Add(T aType)
{
list.Add(aType);
}
public bool ChangeAt(T aType, int index)
{
if (CheckIndex(index))
{
list[index] = aType;
return true;
}
else
{
return false;
}
}
/// <summary>
/// Tittar så det finns ett objekt på platsen som användaren valt
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public bool CheckIndex(int index)
{
if (index < 0)
{
return false;
}
else if (list[index] != null)
{
return true;
}
else
return false;
}
這是我RecipeManager類:
[Serializable]
public class RecipeManager : ListManager<Recipe>
{
/// <summary>
/// Konstruktor
/// </summary>
public RecipeManager()
{
TestData();
}
private void TestData()
{
Recipe testInfo = new Recipe();
testInfo.Name = "AnimalSuperFood";
testInfo.IngredientsList.Add("2dl of water");
testInfo.IngredientsList.Add("3g of meat");
testInfo.IngredientsList.Add("1ml of lemon");
Add(testInfo);
}
}
這是我的食譜類:
[Serializable]
[XmlRoot("Recipe")]
public class Recipe
{
#region props
private ListManager<string> ingredientsList;
[XmlArray("Ingredient")]
public ListManager<string> IngredientsList
{
get { return ingredientsList; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
#endregion
/// <summary>
/// Konstruktor
/// </summary>
public Recipe()
{
ingredientsList = new ListManager<string>();
}
public override string ToString()
{
string strOut = string.Format("{0}: ", Name); ;
for (int i = 0; i < IngredientsList.Count; i++)
{
if (i != (IngredientsList.Count - 1))
{
strOut += string.Format("{0} mixed with ", IngredientsList.GetAt(i));
}
else
{
strOut += string.Format("{0}", IngredientsList.GetAt(i));
}
}
return strOut;
}
}
這是我送我的經理包含對象:
using (SaveFileDialog dlg = new SaveFileDialog())
{
dlg.Title = "Save xml file";
dlg.Filter = "xml file (*.xml)|*.xml";
if (dlg.ShowDialog() == DialogResult.OK)
{
try
{
SerializationUtility.XmlFileSerialize(recipeMgr, dlg.FileName);
MessageBox.Show("Save succesful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
和最後的這是我在哪裏序列化:
public static void XmlFileSerialize<T>(T obj, string filePath)
{
using (Stream s = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
XmlSerializer xmlWriter = new XmlSerializer(typeof(T));
xmlWriter.Serialize(s, obj);
}
}
最後但並非最不重要,請解釋爲什麼我的問題是不好的,如果你這樣認爲。不要只是投下來,離開..我想學習..謝謝!
*爲什麼我的問題很糟糕*因爲您不是什麼都不要求,也不會告訴我們您有什麼問題...... – Gusman
您明白了,因爲RecipeManager是空的,這就是已經序列化了的東西。添加食譜到它... – Gusman
@Gusman我說我的問題是,我不能序列化我的對象。 – Tom