[英]xmlSerialize two lists of different objects
我需要序列化兩個列表並將其寫入xml文件的幫助。 我試過的代碼是
XmlSerializer xs = new XmlSerializer(typeof(List<OtherAction>));
XmlSerializer xsActions = new XmlSerializer(typeof(List<Action>));
using (StreamWriter sw = new StreamWriter("actions.xml"))
{
xs.Serialize(sw, listOtherActions);
xsActions.Serialize(sw, listActions);
}
但是它先寫listOtherActions,然后寫listActions。 因此,它在xml文件上創建了2個根節點。我要做的就是僅包含一個根節點,而我的兩個列表都位於其中。
為什么不創建一個將同時具有兩種List類型的類並序列化該類。 像下面
public class Combined
{
public List<OtherAction> otherActions{get; set;}
public List<Action> actions{get; set;}
}
XmlSerializer xs = new XmlSerializer(typeof(List<Combined>));
using (StreamWriter sw = new StreamWriter("CombinedActions.xml"))
{
xs.Serialize(sw, listCombinedActions);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.