InvalidOperationException in wp7 reading XML without CR between xml declaration and doctype

I upload XML to WP7, and I believe that if I do not have a new line between the XML declaration and doctype, although I ignore doctype, I get an InvalidOperationException . On the desktop, I do not see such an error.

My code is:

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 error is XDocument.Load with an InvalidOperationException and the XmlReader message should not be on a node of type XmlDeclaration. The only difference is the absence of a new line in the second case.

Has anyone seen this before and found a workaround? This works on a desktop computer - just does not work on WP7. In my real case, I am reading XML from a stream, so it’s not so easy to manually enter a new line in the right place.

Damian

+7
source share
2 answers

Now I have implemented a TextReader wrapper that introduces NewLine. I include it here if someone finds it useful, and also if someone has a more elegant solution - if so, please comment!

It uses the XmlReader only to call the Read (...) method to read data - otherwise it throws a NotImplementedException.

In the above example, you will use it as follows:

 textReader = new NewlineAfterXmlDeclReader(new StringReader(fails)); 

This is an implementation

 class NewlineAfterXmlDeclReader : TextReader { private const int InitialChunkSize = 80; private const string SearchText = "?><!" + "DOCTYPE"; //concatenation injected for readability in SO purposes only private static readonly string ReplaceText = "?>" + Environment.NewLine + "<!" + "DOCTYPE"; private readonly TextReader _wrappedReader; private TextReader _firstChunkReader; public NewlineAfterXmlDeclReader(TextReader wrappedReader) { _wrappedReader = wrappedReader; var initialChunk = new char[InitialChunkSize]; var count = _wrappedReader.Read(initialChunk, 0, InitialChunkSize); var initialChunkString = new String(initialChunk, 0, count); _firstChunkReader = new StringReader(initialChunkString.Replace(SearchText, ReplaceText)); } public override int Read(char[] buffer, int index, int count) { var firstChunkReadCount = 0; if (_firstChunkReader != null) { firstChunkReadCount = _firstChunkReader.ReadBlock(buffer, index, count); if (firstChunkReadCount == count) return firstChunkReadCount; _firstChunkReader = null; index += firstChunkReadCount; count -= firstChunkReadCount; } return firstChunkReadCount + _wrappedReader.Read(buffer, index, count); } public override void Close() { _wrappedReader.Close(); } protected override void Dispose(bool disposing) { _wrappedReader.Dispose(); } public override int Peek() { throw new NotImplementedException(); } public override int Read() { throw new NotImplementedException(); } public override string ReadToEnd() { throw new NotImplementedException(); } public override int ReadBlock(char[] buffer, int index, int count) { throw new NotImplementedException(); } public override string ReadLine() { throw new NotImplementedException(); } } 
+3
source

I'm not sure if this is elegant, but it worked for me. I'm not sure what happens if you provide the exact error message, this is the only result!

Code that throws up:

 WebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Timeout = (int)_Timeout.TotalMilliseconds; using (var resp = (HttpWebResponse)request.GetResponse()) { using (XmlReader reader = XmlTextReader.Create(resp.GetResponseStream())) { reader.Read(); return (XElement)XElement.ReadFrom(reader); } } 

Code that pleases:

 WebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Timeout = (int)_Timeout.TotalMilliseconds; using (var resp = (HttpWebResponse)request.GetResponse()) { using (var responseStream = resp.GetResponseStream()) { using (var reader = new StreamReader(responseStream, Encoding.ASCII)) { string raw = reader.ReadToEnd(); return (XElement)XElement.Parse(raw); } } } 

So, for some reason, reading the response into a string and passing it to the Parse () function does something else in using the ReadFrom () function and the stream. Potentially, the Parse function is a little freer, but I haven't dug it yet.

0
source

All Articles