除了我序列化到使用C#的XmlSerializer的XML文件對象的列表,我想存儲在相同的XML幾個獨立的元素(主要是從文本框的字符串)。結合XmlSerializer和XmlWriter?
public static void SaveBehaviors(ObservableCollection<Param> listParams)
{
XmlSerializer _paramsSerializer = new XmlSerializer(listParams.GetType());
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
path += "\\test.xml";
using (TextWriter writeFileStream = new StreamWriter(path))
{
_paramsSerializer.Serialize(writeFileStream, listParams);
using (XmlWriter writer = XmlWriter.Create(writeFileStream))
{
writer.WriteStartElement("Foo"); //test entry...
writer.WriteAttributeString("Bar", "Some & value");
writer.WriteElementString("Nested", "data");
writer.WriteEndElement();
}
}
}
但是,反序列化「test.xml」會因爲添加的元素而導致錯誤。我想在串行化的XML文件中寫入是禁止的,應該避免?
我這樣做,當然第二的其他意見,這是低於建議 - 混合序列化對象和自定義XML賺取你保持的責任跟蹤誰解析什麼,這可能很快成爲巨大的痛苦 –