本文记录了在asp.net mvc项目中使用XmlWriter响应输出XML文档,未调用Close方法关闭流导致在不同的浏览器可能出现的错误提示。
先说明造成此错误的原因,具体可以看MSDN文档:https://msdn.microsoft.com/zh-cn/library/system.xml.xmlwriter.close(v=vs.110).aspx
在使用XmlWriter时,如果在响应之前没有调用Close方法,就会引发InvalidOperationException异常,按照文档所说,是由于使用 XmlWriter 方法输出 XML 时,在调用 Close 方法前,不会写入元素和属性。 例如,如果使用 XmlWriter 写入 XmlDocument,则在关闭 XmlWriter 之前,您将无法在目标文档中看到写入的元素和特性。
下面例举不同浏览器出现的错误提示:
1、This XML file does not appear to have any style information associated with it. The document tree is shown below.
2、This page contains the following errors:
error on line 13 at column 8: Opening and ending tag mismatch: meta line 0 and head
Below is a rendering of the page up to the first error.
3、XML 解析错误:不匹配的记号。预期:</meta>。位置:..行 13,列 3:</head>--^。或者XML 解析错误:未找到元素。
4、Internet Explorer 无法显示此源 此源包含代码错误。XML 文档必须包含一个顶层元素。行:0字符:0 。
作者:十有三
出处:https://shiyousan.com/post/635640131299560082
版权声明:本文采用知识共享许可协议:署名-相同方式共享 4.0 国际(CC BY-SA 4.0)。欢迎转载本文,转载请声明出处或保留此段声明。