XDocument:是否可以强制加载格式错误的 XML 文件?
我有一个格式错误的 XML 文件。根标签不被标签封闭。最终标签丢失。
当我尝试在 C# 中加载格式错误的 XML 文件时
StreamReader sr = new StreamReader(path);
batchFile = XDocument.Load(sr); // Exception
,出现异常“发生意外的文件结尾。以下元素未关闭:批处理。第 54 行,位置 1。”
是否可以忽略关闭标签还是强制加载?我注意到我的所有 XML 工具(如 XML 记事本)都会自动修复或忽略该问题。我无法修复 XML 文件。这是来自第三方软件的一个 COPME,有时该文件是正确的。
I have a malformed XML file. The root tag is not closed by a tag. The final tag is missing.
When I try to load my malformed XML file in C#
StreamReader sr = new StreamReader(path);
batchFile = XDocument.Load(sr); // Exception
I get an exception "Unexpected end of file has occurred. The following elements are not closed: batch. Line 54, position 1."
Is it possible to ignore the close tag or to force the loading? I noticed that all my XML tools ((like XML notepad) ) automaticly fix or ignore the problem. I can not fix the XML file. This one copme from a third party software and sometimes the file is correct.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法使用
XDocument
执行此操作,因为此类会加载内存中的所有文档并完全解析它。但是可以使用
XmlReader
处理文档,它会让您读取和处理完整的文档,最后您会得到缺少标签的异常。You cant do it with
XDocument
because this class loads all document in memory and parse it completly.But its possible to process document with
XmlReader
it would get you to read and process complete document and at the end youll get missing tag exeption.我建议使用 Tidy.NET 来清理混乱的输入
Tidy.NET 有一个很好的 API 来获取问题列表(
MessageCollection
) 在您的“XML”中,您可以使用它来修复内存中的文本流。最简单的事情是一次修复一个错误,但如果错误较多,效果就不会太好。否则,您可能会以相反的文档顺序修复错误,以便在进行修复时消息的偏移量保持有效。下面是一个将 HTML 输入转换为 XHTML 的示例:
Tidy tidy = new Tidy();
I suggest using Tidy.NET to cleanup messy input
Tidy.NET has a nice API to get a list of problems (
MessageCollection
) in your 'XML' and you can use it to fix the text stream in memory. The simplest thing would be to fix one error at a time, thought that will not perform too well with many errors. Otherwise, you might fix errors in reverse document order so that the offsets of messages stay valid while doing the fixesHere is an example to convert HTML input into XHTML:
Tidy tidy = new Tidy();
您可以做的是将结束标记添加到内存中的 xml 中,然后加载它。
因此,将 xml 加载到 Streamreader 后,请在加载 xml 之前操作数据
What you could do is add the closing tag to the xml in memory and then load it.
So after loading the xml into the streamreader, manipulate the data before you do the xml load