<i id='VWug3'><tr id='VWug3'><dt id='VWug3'><q id='VWug3'><span id='VWug3'><b id='VWug3'><form id='VWug3'><ins id='VWug3'></ins><ul id='VWug3'></ul><sub id='VWug3'></sub></form><legend id='VWug3'></legend><bdo id='VWug3'><pre id='VWug3'><center id='VWug3'></center></pre></bdo></b><th id='VWug3'></th></span></q></dt></tr></i><div id='VWug3'><tfoot id='VWug3'></tfoot><dl id='VWug3'><fieldset id='VWug3'></fieldset></dl></div>

      <small id='VWug3'></small><noframes id='VWug3'>

        <tfoot id='VWug3'></tfoot>
          <bdo id='VWug3'></bdo><ul id='VWug3'></ul>
        <legend id='VWug3'><style id='VWug3'><dir id='VWug3'><q id='VWug3'></q></dir></style></legend>
      1. 在 C# 中读取带有未闭合标签的 XML

        时间:2023-08-28
          • <bdo id='o2MMG'></bdo><ul id='o2MMG'></ul>

                  <tbody id='o2MMG'></tbody>
                <tfoot id='o2MMG'></tfoot>

              • <small id='o2MMG'></small><noframes id='o2MMG'>

                  <i id='o2MMG'><tr id='o2MMG'><dt id='o2MMG'><q id='o2MMG'><span id='o2MMG'><b id='o2MMG'><form id='o2MMG'><ins id='o2MMG'></ins><ul id='o2MMG'></ul><sub id='o2MMG'></sub></form><legend id='o2MMG'></legend><bdo id='o2MMG'><pre id='o2MMG'><center id='o2MMG'></center></pre></bdo></b><th id='o2MMG'></th></span></q></dt></tr></i><div id='o2MMG'><tfoot id='o2MMG'></tfoot><dl id='o2MMG'><fieldset id='o2MMG'></fieldset></dl></div>
                  <legend id='o2MMG'><style id='o2MMG'><dir id='o2MMG'><q id='o2MMG'></q></dir></style></legend>
                  本文介绍了在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 C# 中使用 Html 敏捷性解析表格、单元格 下一篇:带有检查空元素的 XML 到 LINQ

                  相关文章

                  最新文章

                      <bdo id='uJypM'></bdo><ul id='uJypM'></ul>

                    <small id='uJypM'></small><noframes id='uJypM'>

                    <i id='uJypM'><tr id='uJypM'><dt id='uJypM'><q id='uJypM'><span id='uJypM'><b id='uJypM'><form id='uJypM'><ins id='uJypM'></ins><ul id='uJypM'></ul><sub id='uJypM'></sub></form><legend id='uJypM'></legend><bdo id='uJypM'><pre id='uJypM'><center id='uJypM'></center></pre></bdo></b><th id='uJypM'></th></span></q></dt></tr></i><div id='uJypM'><tfoot id='uJypM'></tfoot><dl id='uJypM'><fieldset id='uJypM'></fieldset></dl></div>

                    1. <tfoot id='uJypM'></tfoot>
                    2. <legend id='uJypM'><style id='uJypM'><dir id='uJypM'><q id='uJypM'></q></dir></style></legend>