为什么DataContractSerializer的到的StringWriter截断? [英] Why is DataContractSerializer to StringWriter truncated?
问题描述
我现在用的是的DataContractSerializer
连载EF4对象,如果有例外的XML。
在我的调试日志,我可以看到想要是数据内容时出事了。
我有两个版本的code:序列化到一个文件和一个使用该序列化为一个字符串一个版本的StringWriter
在序列化到文件,我得到的约16KB有效的XML大项目。 序列相同项目的字符串时,XML是12KB后截断。任何想法是什么原因造成的截断?
...
VAR实体= ....
SaveAsXml(实体,@C:\ TEMP \ EntityContent.xml); //约16100 BTES确定大小
VAR的xmlString = GetAsXml(实体); //不正常,大小约12200字节
//使舒尔它不是的Debug.WriteLine引起截断
//开始写靠近字符串的结尾
//只有52字节写入,虽然该文件是16101字节长
System.Diagnostics.Debug.Writeline(xml.Substring(12200));
为什么我的字符串被截断任何想法?
下面是code序列化到文件中工程确定
公共静态无效SaveAsXml(对象objectToSave,串filenameWithPath)
{
字符串目录= Path.GetDirectoryName(filenameWithPath);
如果(!Directory.Exists(目录))
{
logger.Debug(按需创建目录+目录);
Directory.CreateDirectory(目录);
}
logger.DebugFormat(编写XML为+ filenameWithPath);
变种DS =新的DataContractSerializer(objectToSave.GetType(),空,Int16.MaxValue,真正的,真实的,NULL);
VAR设置=新XmlWriterSettings
{
缩进= TRUE,
IndentChars =,
NamespaceHandling = NamespaceHandling.OmitDuplicates,
NewLineOnAttributes = TRUE,
};
使用(XmlWriter的W = XmlWriter.Create(filenameWithPath,设置))
{
ds.WriteObject(W,objectToSave);
}
}
这里是code序列化到将被截断字符串</ P>
公共静态字符串GetAsXml(对象objectToSerialize)
{
变种DS =新的DataContractSerializer(objectToSerialize.GetType(),空,Int16.MaxValue,真正的,真实的,NULL);
VAR设置=新XmlWriterSettings
{
缩进= TRUE,
IndentChars =,
NamespaceHandling = NamespaceHandling.OmitDuplicates,
NewLineOnAttributes = TRUE,
};
使用(VAR的StringWriter =新的StringWriter())
{
使用(的XmlWriter的XmlWriter = XmlWriter.Create(StringWriter的,设置))
{
尝试
{
ds.WriteObject(XMLWriter的,objectToSerialize);
返回stringWriter.ToString();
}
赶上(例外前)
{
返回无法序列化'+ objectToSerialize +'为xml:+ ex.Message;
}
}
}
}
在的XmlWriter
的输出,当你调用可能不完全刷新的ToString ()
在的StringWriter
。试着在这之前的的XmlWriter
对象处理:
尝试
{
使用(VAR的StringWriter =新的StringWriter())
{
使用(的XmlWriter的XmlWriter = XmlWriter.Create(StringWriter的,设置))
{
ds.WriteObject(XMLWriter的,objectToSerialize);
}
返回stringWriter.ToString();
}
}
赶上(例外前)
{
返回无法序列化'+ objectToSerialize +'为xml:+ ex.Message;
}
I am using the DataContractSerializer
to serialize EF4 objects to xml if there are exceptions.
In my debug log i can see want was the data-content when something went wrong.
I have two versions of code: one version that serializes to a file and one that serializes to a string using StringWriter
.
When serializing big items to file i get valid xml of about 16kb. when serializing the same item to string the xml is truncated after 12kb. Any idea what caused the truncation?
...
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));
Any ideas why my string is truncated?
Here is the code to serialize to file that works 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);
}
}
here is the code that serializes to string that will be truncated
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;
}
}
}
}
The XmlWriter
's output might not be completely flushed when you invoke ToString()
on the StringWriter
. Try disposing the XmlWriter
object before doing that:
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;
}
这篇关于为什么DataContractSerializer的到的StringWriter截断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!