Missing XML end while serializing with DataContractSerializer

I have the following method that I use to serialize various objects in XML. Then I write the XML to a file. All objects have corresponding attributes [DataContract]and [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();
        }
    }

Serialization works fine, but I miss the end of the content. Ie, the line does not contain the full XML document: the end is truncated. Sometimes a line ends right in the middle of a tag.

It seems that there is no minimum length that could cause the problem: I have lines from 18k that are incomplete, and I have lines from 80k that are also incomplete.

The XML structure is quite simple and contains only about 6-8 nodes.

Did I miss something?

+3
1

xmlWriter , ToString(); :

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

, ToString() Dispose() xmlWriter, , (builder ).

+9

Source: https://habr.com/ru/post/1764106/


All Articles