Read a "fake" XML document (xml fragment) in an XmlTextReader object

[Case] I found a bunch of "xml files" with metadata about a large number of documents in them. At least that's what I asked for. What I got where the "xml files" are without the root element, they are structured something like this (I left a bunch of elements):

<folder name = "abc"></folder>
<folder name = "abc/def">
<document name = "ghi1">
</document>
<document name = "ghi2">
</document>
</folder>

[Problem] When I try to read a file in an XmlTextReader object, it does not tell me that there is no root element.

[Current workaround] Of course, I can read the file as a stream, append <xmlroot> and </xmlroot> and write the stream to a new file and read it in XmlTextReader. This is exactly what I am doing now, but I prefer not to "fake" the source data.

[ ] , XmlTextReader DocumentFragment. :

"System.Xml.XmlException" System.Xml.dll

: XmlNodeType DocumentFragment . 1, 1.

[Faulty code]

using System.Diagnostics;
using System.Xml;

namespace XmlExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string file = @"C:\test.txt";
            XmlTextReader tr = new XmlTextReader(file, XmlNodeType.DocumentFragment, null);
            while(tr.Read())
                Debug.WriteLine("NodeType: {0} NodeName: {1}", tr.NodeType, tr.Name);
        }
    }
}
+2
2

XmlReader ConformanceLevel.Fragment, Martijn, , XmlDataDocument .

, , , , . XML- (XmlDocument, XDocument, XmlDataDocument) TextReader , . :

var dataDocument = new XmlDataDocument();
dataDocument.Load(new FakeRootStreamReader(File.OpenRead("test.xml")));

:

public class FakeRootStreamReader : TextReader
{
    private static readonly char[] _rootStart;
    private static readonly char[] _rootEnd;

    private readonly TextReader _innerReader;
    private int _charsRead;
    private bool _eof;

    static FakeRootStreamReader()
    {
        _rootStart = "<root>".ToCharArray();
        _rootEnd = "</root>".ToCharArray();
    }

    public FakeRootStreamReader(Stream stream)
    {
        _innerReader = new StreamReader(stream);
    }

    public FakeRootStreamReader(TextReader innerReader)
    {
        _innerReader = innerReader;
    }

    public override int Read(char[] buffer, int index, int count)
    {
        if (!_eof && _charsRead < _rootStart.Length)
        {
            // Prepend root element
            return ReadFake(_rootStart, buffer, index, count);
        }

        if (!_eof)
        {
            // Normal reading operation
            int charsRead = _innerReader.Read(buffer, index, count);
            if (charsRead > 0) return charsRead;

            // We've reached the end of the Stream
            _eof = true;
            _charsRead = 0;
        }

        // Append root element end tag at the end of the Stream
        return ReadFake(_rootEnd, buffer, index, count);
    }

    private int ReadFake(char[] source, char[] buffer, int offset, int count)
    {
        int length = Math.Min(source.Length - _charsRead, count);
        Array.Copy(source, _charsRead, buffer, offset, length);
        _charsRead += length;
        return length;
    }
}

Read(...) <root>. , , , .

... meh... , , , - 6 .

+2

:

using System.Diagnostics;
using System.Xml;

namespace XmlExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string file = @"C:\test.txt";
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ConformanceLevel = ConformanceLevel.Fragment;
            using (XmlReader reader = XmlReader.Create(file, settings))
            {
                while (reader.Read())
                    Debug.WriteLine("NodeType: {0} NodeName: {1}", reader.NodeType, reader.Name);
            }
        }
    }
}
+3

Source: https://habr.com/ru/post/1620146/


All Articles