jackjohnson44
asked on
serialize a class write to file
I am going to be using c# to write the contents of a class to a text file. I want to just append to the file each time I write.
I want to get the contents back by reading the classes (all the same) into a generic list.
1. How can I write to the file?
2. How can I read back from the file into a list?
I want to get the contents back by reading the classes (all the same) into a generic list.
1. How can I write to the file?
2. How can I read back from the file into a list?
ASKER
thanks, but what if I keep appending to my text file? Will the list work? What I mean is I am not putting a list into a text file. I am putting classes in a text file and want a list out.
I guess I could read the whole thing into a list, add to the list, then write back, but I would rather just append.
I guess I could read the whole thing into a list, add to the list, then write back, but I would rather just append.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
I am gettting this error:
-------------------------- -
-------------------------- -
System.InvalidOperationExc eption: There is an error in XML document (0, 0). ---> System.Xml.XmlException: Root element is missing.
at System.Xml.XmlTextReaderIm pl.Throw(E xception e)
at System.Xml.XmlTextReaderIm pl.ThrowWi thoutLineI nfo(String res)
at System.Xml.XmlTextReaderIm pl.ParseDo cumentCont ent()
at System.Xml.XmlTextReaderIm pl.Read()
at System.Xml.XmlTextReader.R ead()
at System.Xml.XmlReader.MoveT oContent()
at Microsoft.Xml.Serializatio n.Generate dAssembly. XmlSeriali zationRead erList1.Re ad6_ArrayO fRNetFileE vent()
--- End of inner exception stack trace ---
at System.Xml.Serialization.X mlSerializ er.Deseria lize(XmlRe ader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.X mlSerializ er.Deseria lize(XmlRe ader xmlReader, String encodingStyle)
at System.Xml.Serialization.X mlSerializ er.Deseria lize(TextR eader textReader)
at RNetWordAddIn.ThisAddIn.Ge tFileInfor mation(Doc ument Doc) in C:\Ihi2\SharePoint\OfficeT ools\WordA ddIn\ThisA ddIn.cs:li ne 62
-------------------------- -
OK
-------------------------- -
--------------------------
--------------------------
System.InvalidOperationExc
at System.Xml.XmlTextReaderIm
at System.Xml.XmlTextReaderIm
at System.Xml.XmlTextReaderIm
at System.Xml.XmlTextReaderIm
at System.Xml.XmlTextReader.R
at System.Xml.XmlReader.MoveT
at Microsoft.Xml.Serializatio
--- End of inner exception stack trace ---
at System.Xml.Serialization.X
at System.Xml.Serialization.X
at System.Xml.Serialization.X
at RNetWordAddIn.ThisAddIn.Ge
--------------------------
OK
--------------------------
XmlSerializer xs = new XmlSerializer(typeof(yourL
xs.Serialize(yourfilestrea
if you want to deserialize use:
yourListobject = (yourListClass) xs.Deserialize(yourfilestr