Combining XmlSerializer and XmlWriter?
Posted
by
num3ric
on Stack Overflow
See other posts from Stack Overflow
or by num3ric
Published on 2012-06-04T16:25:51Z
Indexed on
2012/06/04
16:42 UTC
Read the original article
Hit count: 192
c#
In addition to a list of objects I am serializing to an xml file using C#'s XmlSerializer, I would like to store a few more independent elements (mainly strings from textboxes) in the same xml.
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();
}
}
}
However, deserializing "test.xml" results in an error because of the added element. I suppose writing in the serialized xml file is prohibited and should be avoided?
© Stack Overflow or respective owner