Reading XML with unclosed tags in C#

前端 未结 3 695
我寻月下人不归
我寻月下人不归 2021-01-13 23:48

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 repl

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-14 00:26

    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.

    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.

    Sample code:

    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);
                }
            }
        }
    }
    

    Sample XML:

    
      
        First child
        Second child
        Broken
    

    Sample output:

    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.

提交回复
热议问题