Saturday, 15 September 2012

insert xml node without XmlDomument.Load() in c#+XML -



insert xml node without XmlDomument.Load() in c#+XML -

have log.xml file nodes

<appxmllogwritter> <logdata> <logid>5678201302071614556349583069537201151138</logid> <logdatetime>20130207161455</logdatetime> <logtype>warning</logtype> <logflag>baburaologflag</logflag> <logapplication>baburaologapplication</logapplication> <logmodule>baburaologmodule</logmodule> <loglocation>baburaologlocation</loglocation> <logtext>baburaologtext</logtext> </logdata> <logdata> <logid>5678201302071614556349583069537201151138</logid> <logdatetime>20130207161455</logdatetime> <logtype>warning</logtype> <logflag>baburaologflag</logflag> <logapplication>baburaologapplication</logapplication> <logmodule>baburaologmodule</logmodule> <loglocation>baburaologlocation</loglocation> <logtext>baburaologtext</logtext> </logdata> </appxmllogwritter>

i want add together few new node replacing root node <appxmllogwritter> in xml file. without overwriting older node. code replace old node new node want preserve old node new node

mutex objmutex = new mutex(false, @"global\mysharedlog"); xmldocument xmldoc = new xmldocument(); string currentdatetime = datetime.now.tostring("yyyymmddhhmmss"); xmlelement newelement = xmldoc.createelement("logdata"); xmlelement xmllogid = xmldoc.createelement("logid"); xmlelement xmllogdatetime = xmldoc.createelement("logdatetime"); xmlelement xmllogtype = xmldoc.createelement("logtype"); xmlelement xmllogflag = xmldoc.createelement("logflag"); xmlelement xmllogapplication = xmldoc.createelement("logapplication"); xmlelement xmllogmodule = xmldoc.createelement("logmodule"); xmlelement xmlloglocation = xmldoc.createelement("loglocation"); xmlelement xmllogtext = xmldoc.createelement("logtext"); xmlelement xmllogstacktrace = xmldoc.createelement("logstacktrace"); int randomnumber = random.next(9999); xmllogid.innertext = _logidprefix + currentdatetime + datetime.utcnow.ticks + randomnumber; xmllogdatetime.innertext = currentdatetime; xmllogtype.innertext = ((logtypes)convert.toint32(logtype)).tostring(); xmllogflag.innertext = logflag; xmllogapplication.innertext = _logapplication; xmllogmodule.innertext = logmodule; xmlloglocation.innertext = loglocation; xmllogtext.innertext = logtext; xmllogstacktrace.innertext = logstacktrace; newelement.appendchild(xmllogid); newelement.appendchild(xmllogdatetime); newelement.appendchild(xmllogtype); newelement.appendchild(xmllogflag); newelement.appendchild(xmllogapplication); newelement.appendchild(xmllogmodule); newelement.appendchild(xmlloglocation); newelement.appendchild(xmllogtext); seek { objmutex.waitone(); if (!file.exists(_logfilepath)) { file.writealltext(_logfilepath, "<?xml version='1.0' encoding='utf-16' standalone='yes'?>\n<appxmllogwritter>\n</appxmllogwritter>"); } string filecontains = string.empty; foreach (string line in file.readlines(_logfilepath)) { if (line.contains("<appxmllogwritter>")) { filecontains = line.replace("<appxmllogwritter>", "<appxmllogwritter>" + newelement.outerxml.tostring()); break; } } //xmldoc.preservewhitespace = true; xmldoc.loadxml(filecontains); // //xmldoc.load(_logfilepath); xmlelement ele = xmldoc.documentelement; xmldoc.documentelement.appendchild(newelement); xmldoc.save(_logfilepath); } { objmutex.releasemutex(); }

ie both old node , new node save file without replacing old node.

i have solve issue become much more faster load method of xmldocument code replace seek block correctly work , remainind code is

string rootendtag = "</appxmllogwritter>"; int rootendtaglength = encoding.utf8.getbytes(rootendtag).length; seek { objmutex.waitone(); if (!file.exists(_logfilepath)) { file.writealltext(_logfilepath, "<?xml version='1.0' encoding='utf-8' standalone='yes'?><appxmllogwritter></appxmllogwritter>"); } var filestream = file.open(_logfilepath, filemode.open); filestream.position = filestream.length - rootendtaglength; var objstreamwriter = new streamwriter(filestream); objstreamwriter.writeline(newelement.outerxml); objstreamwriter.write(rootendtag); objstreamwriter.close(); filestream.close(); }

c# xml

No comments:

Post a Comment