简体   繁体   English

使用DataContractSerializer进行序列化时缺少XML的结尾

[英]Missing the end of the XML when serializing with DataContractSerializer

I have the following method that I use to serialize various objects to XML. 我有以下方法用于将各种对象序列化为XML。 I then write the XML to a file. 然后我将XML写入文件。 All the objects have the proper [DataContract] and [DataMember] attributes. 所有对象都具有适当的[DataContract][DataMember]属性。

    public static string Serialize<T>(T item)
    {
        var builder = new StringBuilder();
        var serializer = new DataContractSerializer(typeof(T));

        using (var xmlWriter = XmlWriter.Create(builder))
        {
            serializer.WriteObject(xmlWriter, item);
            return builder.ToString();
        }
    }

The serialization works fine, however, I am missing the end of the content. 序列化工作正常,但是,我错过了内容的结尾。 Ie, the string does not contain the full XML document: the end gets truncated. 即,字符串不包含完整的XML文档:结尾被截断。 Sometimes the string ends right in the middle of a tag. 有时,字符串会在标记的中间结束。

There does not seem to be a miximum length that would cause an issue: I have strings of 18k that are incomplete and I have strings of 80k that are incomplete as well. 似乎没有混合长度会导致问题:我有18k的字符串是不完整的,我有80k的字符串也是不完整的。

The XML structure is fairly simple and only about 6-8 nodes deep. XML结构非常简单,只有大约6-8个节点。

Am I missing something? 我错过了什么吗?

xmlWriter isn't flushed at the point you call ToString() ; 在调用ToString() ,不会刷新xmlWriter ; try: 尝试:

    using (var xmlWriter = XmlWriter.Create(builder))
    {
        serializer.WriteObject(xmlWriter, item);
    }
    return builder.ToString();

This does the ToString() after the Dispose() on xmlWriter , meaning it will flush any buffered data to the output ( builder in this case). 这在xmlWriter上的Dispose() ToString() 之后执行ToString() ,这意味着它会将任何缓冲的数据刷新到输出(在本例中为builder )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用DataContractSerializer进行序列化时出现SecurityException - SecurityException when serializing with DataContractSerializer .Net DataContractSerializer的往返XML序列化失败 - Round trip XML serializing with .Net DataContractSerializer fails 实体未使用DataContractSerializer进行序列化 - Entities are not serializing using DataContractSerializer 删除使用DataContractSerializer序列化的名称空间 - Removing namespaces serializing with DataContractSerializer 序列化对象以进行SHA1加密时出现DataContractSerializer错误 - DataContractSerializer error when serializing a object for SHA1 encryption 使用 DataContractSerializer 进行序列化时如何忽略属性? - How can I ignore a property when serializing using the DataContractSerializer? 使用DataContractSerializer序列化大对象图时出现C#StackOverFlowException - C# StackOverFlowException when serializing large object graph with DataContractSerializer DataContractSerializer未序列化一个属性 - DataContractSerializer not serializing one property XML序列化 - 何时使用DataContractSerializer / Binary / XMLSerialiser - XML Serialisation - When To Use DataContractSerializer / Binary / XMLSerialiser 使用DataContractSerializer序列化接口列表 - Serializing a List of Interfaces using the DataContractSerializer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM