I have an XmlReader that is trying to read text into a list of elements. I am having trouble getting it to reader the text: "a [ z ]". If I try with the text "a [ z ] " (same but with two trailing spaces) it works fine. Below is an example:
TextReader tr = new StringReader("a [ z ]");
XmlReaderSettings settings = new XmlReaderSettings
{
ConformanceLevel = ConformanceLevel.Fragment,
ProhibitDtd = false,
ValidationType = ValidationType.None,
XmlResolver = null,
CheckCharacters = false,
IgnoreProcessingInstructions = true,
};
XmlReader reader = XmlReader.Create(tr, settings);
reader.Read();
StringBuilder sb = new StringBuilder();
while (!reader.EOF)
{
if (reader.NodeType == XmlNodeType.Text || reader.NodeType == XmlNodeType.Whitespace)
{
sb.Append(reader.Value);
reader.Read();
}
}
// sb.ToString() should be "a [ z ]"
When you run it fails with the message: "System.Xml.XmlException : Unexpected end of file has occurred. Line 1, position 7." and a stack trace:
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars)
at System.Xml.XmlTextReaderImpl.FinishPartialValue()
at System.Xml.XmlTextReaderImpl.get_Value()
at LocalisationFormats.Tests.Shared.InlineElements.InlineElementHelperTest.Test()
When you attempt to debug it, the Reader is in a ReadState of "Error" and the Reader.Value is "a [ z", and then you break the reader and get an OutOfMemoryExecption.
Anyone any suggestions?
EDIT: removed extra if block from code snippet on suggestion from Gregoire.