Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

c# - Consecutive XML serialization causes - Token StartElement in state EndRootElement would result in an invalid XML document

I am writing to the file XML serialization of the object, generated by validator.MatchPossiblyValid(string input)method. First call, serializes and write to the file. However, the second call fails with an exception: System.InvalidOperationException: 'Token StartElement in state EndRootElement would result in an invalid XML document. Make sure that the ConformanceLevel setting is set to ConformanceLevel.Fragment or ConformanceLevel.Auto if you want to write an XML fragment. '

XmlSerializerNamespaces emptyNS = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
var serializer = new XmlSerializer(typeof(PDPCustomerInfoInvalid));
var settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
//settings.Indent = true;
using (var stream = new System.IO.StreamWriter(args[1], true))
{
    using (var writer = XmlWriter.Create(stream, settings))
        {

                serializer.Serialize(writer, validator.MatchPossiblyValid("STRING FOR PARSING"), emptyNS);
                stream.Write(Environment.NewLine);
                stream.Flush();
                //Line below throws the exception
                serializer.Serialize(writer, validator.MatchPossiblyValid("STRING FOR PARSING"), emptyNS);
                stream.Write(Environment.NewLine);
                stream.Flush();

        }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are trying to use a single XmlWriter to create an XML file with multiple root elements. However, the XML standard requires exactly one root element per XML document. Your XmlWriter is throwing the exception to indicate that the XML being created is invalid. (MCVE here.)

If you really need to concatenate two XML documents into a single file, you could use separate XmlWriters created with XmlWriterSettings.CloseOutput set to false:

using (var stream = new System.IO.StreamWriter(args[1], true))
{
    var settings = new XmlWriterSettings();
    settings.OmitXmlDeclaration = true;
    //settings.Indent = true;
    settings.CloseOutput = false;

    using (var writer = XmlWriter.Create(stream, settings))
    {
        serializer.Serialize(writer, validator.MatchPossiblyValid("STRING FOR PARSING"), emptyNS);
    }

    stream.Write(Environment.NewLine);
    stream.Flush();

    using (var writer = XmlWriter.Create(stream, settings))
    {
        serializer.Serialize(writer, validator.MatchPossiblyValid("STRING FOR PARSING"), emptyNS);
    }
    //Line below throws the exception
    stream.Write(Environment.NewLine);
    stream.Flush();             
}

Sample fiddle.

Or, better yet, don't do this at all, since an "XML Document" with multiple roots is, as stated above, not valid. Instead, serialize both objects inside some container element.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...