在 C# 中读取带有未闭合标签的 XML

Reading XML with unclosed tags in C#(在 C# 中读取带有未闭合标签的 XML)
本文介绍了在 C# 中读取带有未闭合标签的 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个程序可以运行测试并生成一个包含所有结果的网格视图,以及一个 XML 日志文件.该程序还具有加载日志以复制网格视图的功能.

I have a program which runs tests and generates a grid-view with all the results in it, and also an XML log file. The program also has the functionality to load logs to replicate the grid-view.

由于程序在执行时写入日志文件,如果它崩溃,日志文件将缺少结束标记.我仍然希望能够加载这些 XML 文件,因为仍然有很多有价值的数据可以帮助我找出导致崩溃的原因.

Since the program writes to the log file as its executing, if it crashes the log file will be missing closing tags. I still want to be able to load these XML files though as there is still lots of valuable data that can help me find out what caused the crash.

我在想也许可以通过 XML 文件并关闭任何未关闭的 XML 标记,或者可能编写某种脏"XML 阅读器来假装每个标记都已关闭.关于我可以做什么或应该如何进行的任何想法?

I was thinking maybe going through the XML file and closing off any unclosed XML tag, or maybe writing some kind of "Dirty" XML reader that would pretend every tag was closed. Any ideas on what I could do or how I should proceed?

<Root>
  <Parent>
     <Child Name="One">
        <Foo>...</Foo>
        <Bar>...</Bar>
        <Baz>...</Baz>
     </Child>
     <Child Name="Two">
        <Foo>...</Foo>
        <Bar>...</Bar>
 !-- Crash happens here --!

从此我仍然会期待生产

 Child   Foo   Bar   Baz
 One     ...   ...   ...
 Two     ...   ...    /

推荐答案

大概它在被截断之前都是有效的......所以使用 XmlReader 可以工作......只是准备好处理它吧当它到达截断点时.

Presumably it's all valid until it's truncated... so using XmlReader could work... just be prepared to handle it going bang when it reaches the truncation point.

现在 XmlReader API 不是特别好 (IMO),所以您可能想要移动到一些有趣数据的开头(这些数据本身必须是完整的),然后调用 XNode.ReadFrom(XmlReader) 方法以简单易用的形式获取该数据.然后移动到下一个元素的开头并执行相同的操作,等等.

Now the XmlReader API isn't terribly pleasant (IMO) so you might want to move to the start of some interesting data (which would have to be complete in itself) and then call the XNode.ReadFrom(XmlReader) method to get that data in a simple-to-use form. Then move to the start of the next element and do the same, etc.

示例代码:

using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

class Program
{
    static void Main(string[] args)
    {
        using (XmlReader reader = XmlReader.Create("test.xml"))
        {
            while (true)
            {
                while (reader.NodeType != XmlNodeType.Element ||
                    reader.LocalName != "Child")
                {
                    if (!reader.Read())
                    {
                        Console.WriteLine("Finished!");
                    }
                }
                XElement element = (XElement) XNode.ReadFrom(reader);
                Console.WriteLine("Got child: {0}", element.Value);
            }
        }
    }
}

示例 XML:

<Root>
  <Parent>
    <Child>First child</Child>
    <Child>Second child</Child>
    <Child>Broken

样本输出:

得到孩子:第一个孩子得到孩子:第二个孩子

Got child: First child Got child: Second child

Unhandled Exception: System.Xml.XmlException: Unexpected end of file has occurred
The following elements are not closed: Child, Parent, Root. Line 5, position 18.
   at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
   at System.Xml.XmlTextReaderImpl.ParseElementContent()
   at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r)
   at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o)
   at System.Xml.Linq.XElement.ReadElementFrom(XmlReader r, LoadOptions o)
   at System.Xml.Linq.XNode.ReadFrom(XmlReader reader)
   at Program.Main(String[] args)

所以很明显你想捕捉异常,但你可以看到它成功地读取了前两个元素.

So obviously you'd want to catch the exception, but you can see that it managed to read the first two elements correctly.

这篇关于在 C# 中读取带有未闭合标签的 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Custom Error Queue Name when using EasyNetQ for RabbitMQ?(使用 EasyNetQ for RabbitMQ 时自定义错误队列名称?)
How to generate password_hash for RabbitMQ Management HTTP API(如何为 RabbitMQ 管理 HTTP API 生成密码哈希)
Rabbitmq Ack or Nack, leaving messages on the queue(Rabbitmq Ack 或 Nack,将消息留在队列中)
Setup RabbitMQ consumer in ASP.NET Core application(在 ASP.NET Core 应用程序中设置 RabbitMQ 消费者)
Specify Publish timeouts in mass transit(指定公共交通中的发布超时)
RabbitMQ asynchronous support(RabbitMQ 异步支持)