Given the following source code:
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
namespace TheXMLGames
{
class Program
{
static void Main(string[] args)
{
XmlReaderSettings settings = new XmlReaderSettings {
Async = false,
ConformanceLevel = ConformanceLevel.Fragment,
DtdProcessing = DtdProcessing.Ignore,
ValidationFlags = XmlSchemaValidationFlags.None,
ValidationType = ValidationType.None,
XmlResolver = null,
};
string head = File.ReadAllText("sample.xml");
Stream stringStream = GenerateStreamFromString(head);
// Variant 1
//XmlReader reader = XmlReader.Create(stringStream);
// Variant 2
//XmlReader reader = XmlReader.Create(stringStream, settings);
// Variant 3
XmlTextReader reader = new XmlTextReader(stringStream);
while (reader.Read())
if (reader.NodeType != XmlNodeType.Whitespace)
Console.WriteLine(reader.Name + ": " + reader.Value);
// No Variant gets here without an exception,
// but that's not the point!
Console.ReadKey();
}
public static Stream GenerateStreamFromString(string s)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
}
}
sample.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TestingFacility >
<TestingFacility id="MACHINE_2015-11-11T11_11_11" version="2015-11-11">
<Program>
<Title>title</Title>
<Steps>16</Steps>
</Program>
<Calibration>
<Current offset="0" gain="111.11" />
<Voltage offset="0" gain="111.11" />
</Calibration>
<Info type="Facilityname" value="MACHINE" />
<Info type="Hardwareversion" value="HW11" />
<Info type="Account" value="DJohn" />
<Info type="Teststart" value="2015-11-11T11:11:11" />
<Info type="Description" value="desc" />
<Info type="Profiler" value="prof" />
<Info type="Target" value="trgt" />
The behaviour is the following:
Variant 1
XmlReader.Create(stream)
An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll
Additional information: For security reasons DTD is prohibited in this XML document. To enable DTD processing set the DtdProcessing property on XmlReaderSettings to Parse and pass the settings into XmlReader.Create method.
Variant 2
XmlReader.Create(stream, settings)
An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll
Additional information: Unexpected DTD declaration. Line 2, position 3.
Variant 3
new XmlTextReader(stringStream)
An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll
Additional information: Unexpected end of file has occurred. The following elements are not closed: TestingFacility. Line 19, position 36.
Variant 1 and 2 throw after the first line.
Variant 3 outputs the whole file as expected and when it gets to the end, it complains (correctly!).
The software works as I obviously use Variant 3, but the (now) recommended way is to use the Factory via XmlReader.Create
If I fiddle with the settings, it starts getting even more weird.
How can I get the code up-to-date and use XmlReader.Create?
The full project can be found here: https://drive.google.com/file/d/0B55cC50M31_8T0lub25oS2QxQ00/view