连续的 XML 序列化原因 - 处于 EndRootElement 状态的令牌 StartElement 将导致无效的 XML 文档 [英] Consecutive XML serialization causes - Token StartElement in state EndRootElement would result in an invalid XML document
问题描述
我正在写入对象的 XML 序列化文件,由 validator.MatchPossivelyValid(string input)
方法生成.第一次调用,序列化并写入文件.但是,第二次调用失败并出现异常:System.InvalidOperationException: 'Token StartElement in state EndRootElement 将导致无效的 XML 文档.如果要编写 XML 片段,请确保将 ConformanceLevel 设置设置为 ConformanceLevel.Fragment 或 ConformanceLevel.Auto.'
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();
}
}
推荐答案
您正在尝试使用单个 XmlWriter
来创建具有多个 根元素.但是,XML 标准要求正好是一个根元素 每个 XML 文档.您的 XmlWriter
正在抛出异常以指示正在创建的 XML 无效.(MCVE 此处.)
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.)
如果您确实需要将两个 XML 文档连接到一个文件中,您可以使用由 XmlWriterSettings.CloseOutput
设置为 false
:
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();
}
示例 fiddle.
或者,更好的是,根本不要这样做,因为如上所述,具有多个根的XML 文档"是无效的.相反,在某个容器元素内序列化两个对象.
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.
这篇关于连续的 XML 序列化原因 - 处于 EndRootElement 状态的令牌 StartElement 将导致无效的 XML 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!