c# - How do I fix the CA2202 warning? -


below method use parse xml. giving ca2202 warning on code analysis says object mstream can disposed multiple times , should not call dispose more once. how can solve warning?

public static string printxml(string xml)     {         string result = "";         string[] xmlseperators = new string[] { "<?" };         string[] splitresults = new string[2];          if (!string.isnullorempty(xml))         {             using (memorystream mstream = new memorystream())             {                 using (xmltextwriter writer = new xmltextwriter(mstream, encoding.unicode))                 {                     xmldocument document = new xmldocument();                     try                     {                         // load xmldocument xml.                         //check if xml                          if (xml.startswith("<?"))                         {                             document.loadxml(xml);                         }                         else                         {                             //split string appended before xml                             splitresults = xml.split(xmlseperators, 2, stringsplitoptions.none);                             if (splitresults.length > 1)                             {                                 string d = "<?" + splitresults[1];                                 document.loadxml(d);                             }                         }                         writer.formatting = formatting.indented;                         // write xml formatting xmltextwriter                         document.writecontentto(writer);                         //xx.writeto(writer);                         writer.flush();                         mstream.flush();                         // have rewind memorystream in order read contents.                         mstream.position = 0;                         // read memorystream contents streamreader.                         streamreader sreader = new streamreader(mstream);                         // extract text streamreader.                         string formattedxml = sreader.readtoend();                          if (splitresults[0] != null)                         {                             result = splitresults[0] + "\n" + formattedxml;                         }                         else                         {                             result = formattedxml;                         }                     }                     catch (xmlexception xe)                     {                         log.error(xe);                         throw;                     }                                         }                                }         }         return result;     } 

the reason getting warning xmltextwriter.dispose() ensure under lying memorystream object disposed. when using scope of memorystream ends, try disposing memorystream object , hence warning.

using block compiles try-finally block. inner using block in code call dispose on writer. call dispose on memorystream object mstream. @ exit of control inner using block, outer using block try dispose object writer, since has been disposed, getting warning on code analysis tool.

to rid of warning, can remove first using statement , use try-finally block. but remember set mstream null enter second using statement. has been explained @ ca2202: not dispose objects multiple times

your code like:

public static string printxml(string xml) {     string result = "";     string[] xmlseperators = new string[] { "<?" };     string[] splitresults = new string[2];      if (!string.isnullorempty(xml))     {         memorystream mstream  = null;         try         {             mstream = new memorystream();             using (xmltextwriter writer = new xmltextwriter(mstream, encoding.unicode))             {                 mstream = null; // important                  xmldocument document = new xmldocument();                 try                 {                     // load xmldocument xml.                     //check if xml                      if (xml.startswith("<?"))                     {                         document.loadxml(xml);                     }                     else                     {                         //split string appended before xml                         splitresults = xml.split(xmlseperators, 2, stringsplitoptions.none);                         if (splitresults.length > 1)                         {                             string d = "<?" + splitresults[1];                             document.loadxml(d);                         }                     }                     writer.formatting = system.xml.formatting.indented;                     // write xml formatting xmltextwriter                     document.writecontentto(writer);                     //xx.writeto(writer);                     writer.flush();                     mstream.flush();                     // have rewind memorystream in order read contents.                     mstream.position = 0;                     // read memorystream contents streamreader.                     streamreader sreader = new streamreader(mstream);                     // extract text streamreader.                     string formattedxml = sreader.readtoend();                      if (splitresults[0] != null)                     {                         result = splitresults[0] + "\n" + formattedxml;                     }                     else                     {                         result = formattedxml;                     }                 }                 catch (xmlexception xe)                 {                     log.error(xe);                     throw;                 }             }         }                  {              if (mstream != null)             {                 mstream.dispose();             }          }     }     return result; } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -