c# - Combining XmlSerializer and XmlWriter? -


in addition list of objects serializing xml file using c#'s xmlserializer, store few more independent elements (mainly strings textboxes) in 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 error because of added element. suppose writing in serialized xml file prohibited , should avoided?

no. don't that.

if need serialize more observablecollection, define containing type , serialize that.

public static void savebehaviors(observablecollection<param> listparams)  {      xmlserializer _paramsserializer = new xmlserializer(typeof(containingtype));      var c = new containingtype(listparams);      c.extrainformation = whatever....;       string path = environment.getfolderpath(environment.specialfolder.desktop);      path += "\\test.xml";      using (textwriter writefilestream = new streamwriter(path))      {          _paramsserializer.serialize(writefilestream, c);      }  }  

use xmlserializer.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -