InvalidOperationException on wp7 reading XML with no CR between xml declaration and doctype

I'm loading XML on WP7, and I find that if I don't have a newline between the XML declaration and the doctype, even though I am ignoring the doctype, I get an InvalidOperationException. On the desktop I get no such error.

My code:

private static void Example()
{
    const string works =
        @"<?xml version=""1.0""?>
<!DOCTYPE example SYSTEM ""http://example.com/example.dtd""><hello></hello>";

    const string fails =
        @"<?xml version=""1.0""?><!DOCTYPE example SYSTEM ""http://example.com/example.dtd""><hello></hello>";

    var textReader = new StringReader(works);  
    var xmlReaderSettings = new XmlReaderSettings {DtdProcessing = DtdProcessing.Ignore,};
    var xmlReader = XmlReader.Create(textReader, xmlReaderSettings);
    XDocument.Load(xmlReader);  // No problem here

    textReader = new StringReader(fails);  

    xmlReader = XmlReader.Create(textReader, xmlReaderSettings);
    XDocument.Load(xmlReader);  // Fails here
}

The second XDocument.Load fails with an InvalidOperationException and the message The XmlReader should not be on a node of type XmlDeclaration. The only difference is the lack of a new line in the second case.

Has anyone seen this before, and found a workaround? This works on the desktop btw - just fails on WP7. In my real case I am reading the XML from a stream, so it won't be so easy to manually inject the new line at the right place.

Damian

7
задан Damian 5 May 2011 в 19:25
поделиться