問題描述
我有一個程序可以運行測試并生成一個包含所有結果的網格視圖,以及一個 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模板網!