I want to detect the encoding of a XML document before parsing it. So I found on stackoverflow this script.
public static XElement GetXMLFromStream(Stream uploadStream)
{
/** Remember position */
var position = uploadStream.Position;
/** Get encoding */
var xmlReader = new XmlTextReader(uploadStream);
xmlReader.MoveToContent();
/** Move to remembered position */
uploadStream.Seek(position, SeekOrigin.Begin); // with "pos" = 0 it not works, too
uploadStream.Seek(position, SeekOrigin.Current); // if I remove this I have the same issue!
/** Read content with detected encoding */
var streamReader = new StreamReader(uploadStream, xmlReader.Encoding);
var streamReaderString = streamReader.ReadToEnd();
return XElement.Parse(streamReaderString);
}
But it doesn't work.
Always I get EndOfStream
true. But it isn't!!!! -.-
For example I have the string <test></test>
.
Begin: 0, End: 13
If I ReadToEnd
or MoveToContent
then the end is reached successfully. The EndOfStream
is true then.
If I reset the position via Seek
or Position
to 0 (for example) then a new StreamReader
shows always EndOfStream
is true.
The thing is that the uploadStream
is a stream which I can not close.
It's a SharpZipLib stream of a http upload stream. So I can't close this stream. I can only working with it.
And the bad thing is only because Position
and Seek
not work... Only because ReadToEnd
relays on this Position
. - Else it would work. I think!
Maybe you can help my with this situation :-)
Thank you very much in Advance!