5

J'utilise le DataContractSerializer pour sérialiser des objets EF4 en xml s'il y a des exceptions. Dans mon journal de débogage, je peux voir le contenu des données quand quelque chose s'est mal passé.Pourquoi DataContractSerializer to StringWriter est-il tronqué?

J'ai deux versions de code: une version sérialisable en un fichier et une qui sérialise en une chaîne en utilisant StringWriter.

Lors de la sérialisation de gros éléments dans un fichier, je reçois xml valide d'environ 16 Ko. lors de la sérialisation du même élément en chaîne, le fichier XML est tronqué après 12 Ko. Une idée de ce qui a causé la troncature?

... 
    var entity = .... 
    SaveAsXml(entity, @"c:\temp\EntityContent.xml"); // ok size about 16100 btes 
    var xmlString = GetAsXml(entity); // not ok, size about 12200 bytes 

    // to make shure that it is not Debug.Writeline that causes the truncation 
    // start writing near the end of the string 
    // only 52 bytes are written although the file is 16101 bytes long 
    System.Diagnostics.Debug.Writeline(xml.Substring(12200)); 

Des idées pour lesquelles ma chaîne est tronquée?

Voici le code de sérialisation de fichier qui fonctionne ok

public static void SaveAsXml(object objectToSave, string filenameWithPath) 
    { 
    string directory = Path.GetDirectoryName(filenameWithPath); 
    if (!Directory.Exists(directory)) 
    { 
     logger.Debug("Creating directory on demand " + directory); 
     Directory.CreateDirectory(directory); 
    } 

    logger.DebugFormat("Writing xml to " + filenameWithPath); 
    var ds = new DataContractSerializer(objectToSave.GetType(), null, Int16.MaxValue, true, true, null); 

    var settings = new XmlWriterSettings 
    { 
     Indent = true, 
     IndentChars = " ", 
     NamespaceHandling = NamespaceHandling.OmitDuplicates, 
     NewLineOnAttributes = true, 
    }; 
    using (XmlWriter w = XmlWriter.Create(filenameWithPath, settings)) 
    { 
     ds.WriteObject(w, objectToSave); 
    } 
    } 

est le code ici qui sérialise à chaîne qui sera tronquée

public static string GetAsXml(object objectToSerialize) 
    { 
    var ds = new DataContractSerializer(objectToSerialize.GetType(), null, Int16.MaxValue, true, true, null); 
    var settings = new XmlWriterSettings 
    { 
     Indent = true, 
     IndentChars = " ", 
     NamespaceHandling = NamespaceHandling.OmitDuplicates, 
     NewLineOnAttributes = true, 
    }; 
    using (var stringWriter = new StringWriter()) 
    { 
     using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, settings)) 
     { 
      try 
      { 
       ds.WriteObject(xmlWriter, objectToSerialize); 
       return stringWriter.ToString(); 
      } 
      catch (Exception ex) 
      { 
       return "cannot serialize '" + objectToSerialize + "' to xml : " + ex.Message; 
      } 
     } 
    } 
    } 

Répondre

8

La sortie de XmlWriter pourrait ne pas être complètement Rincé lorsque vous appelez ToString() sur le StringWriter. Essayez de jeter l'objet XmlWriter avant de faire cela:

try 
{ 
    using (var stringWriter = new StringWriter()) 
    { 
     using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, settings)) 
     { 
      ds.WriteObject(xmlWriter, objectToSerialize); 
     } 
     return stringWriter.ToString(); 
    } 
} 
catch (Exception ex) 
{ 
    return "cannot serialize '" + objectToSerialize + "' to xml : " + ex.Message; 
} 
+1

+1: merci pour la solution de travail. Au lieu de disposer de 'xmlWriter.Flush();' avant 'return stringWriter.ToString();' fonctionne aussi bien. – k3b

Questions connexes