Reading Data Sets from XML c# -


i have simple class designing log messages on hosted website.

to store messages, have created simple class structure:

public class storedmsg {   public storedmsg() {   }   public string { get; set; }   public string date { get; set; }   public string message { get; set; } } 

these messages written , read using storedmessages : list<storedmsg> class.

this original format of read method:

  public void read() {     //string xmlpath = webpage.server.mappath(xml_path);     if (file.exists(xmlpath)) {       using (var xr = new xmltextreader(xmlpath)) {         storedmsg item = null;         while (xr.read()) {           if (xr.nodetype == xmlnodetype.element) {             if (xr.name == head) {               if (item != null) {                 this.add(item);               }               item = new storedmsg();             } else if (xr.name == from) {               item.from = xr.readelementstring();             } else if (xr.name == date) {               item.date = xr.readelementstring();             } else if (xr.name == message) {               item.message = xr.readelementstring();             }           }         }         xr.close();       }     }   } 

the write method in format:

  public void write(storedmsg item) {     using (var stream = file.open(xmlpath, filemode.append, fileaccess.write, fileshare.read)) {       using (var xw = new system.xml.xmltextwriter(stream, encoding.utf8)) {         xw.writestartelement(head);         {           xw.writewhitespace("\r\n\t");           xw.writeelementstring(from, item.from);           xw.writewhitespace("\r\n\t");           xw.writeelementstring(date, item.date);           xw.writewhitespace("\r\n\t");           xw.writeelementstring(message, item.message);           xw.writewhitespace("\r\n");         }         xw.writeendelement();         xw.writewhitespace("\r\n");       }       stream.close();     }   } 

the problem read method did not in structure of xml, list empty.

it hard troubleshoot on hosted website, started looking online @ examples see might have been doing wrong.

during search, found microsoft's how to: write object data xml file (c# , visual basic) showed me simple way streamline write method had done:

private xmlserializer serializer;  public storedmessages() {   serializer = new xmlserializer(typeof(storedmsg)); }  public void write(storedmsg item) {   using (var file = new streamwriter(xmlpath, true)) {     serializer.serialize(file, item);   } } 

that, me, simplicity , elegance!

being happy this, went on microsoft's how to: read object data xml file (c# , visual basic), implement well.

here problem:

the best can seem write method this:

public void read() {   if (file.exists(xmlpath)) {     using (var file = new streamreader(xmlpath)) {       // how handle multiple nodes?       // http://msdn.microsoft.com/en-us/library/vstudio/ms172872.aspx       var item = (storedmsg)serializer.deserialize(file);       this.add(item);     }   } } 

i interested in seeing show me how read of items in linq xml , give +1 that, question here how read of data in using read technique above.

is incorrectly casting deserialized object storedmsg instead of else? if so, something else be?

  • storedmsg[]?
  • list<object>?
  • storedmessages?

thanks reading of this, , hope code legible others.

example data provided screenshot prevent others grabbing email address easily:

screenshot

here more generic way, no can pass object read/write

void write<t>(stream s,t obj) {     var serializer = new xmlserializer(typeof(t));     serializer.serialize(s, obj); }  t read<t>(stream s) {     var serializer = new xmlserializer(typeof(t));     return (t)serializer.deserialize(s); } 

for example

storedmsg[] msgs = .....; write(stream, msgs); 

or

storedmsg[] msgs = read<storedmsg[]>(stream()); 

edit

void write<t>(string filename, t obj) {     using (var stream = file.create(filename))     {         write(stream, obj);     } }  t read<t>(string filename) {     using (var stream = file.open(filename, filemode.open))     {         return read<t>(stream);     } } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -