I want to determine the encoding of an XML document before parsing it. So I found this script overflow on the stack .
public static XElement GetXMLFromStream(Stream uploadStream)
{
var position = uploadStream.Position;
var xmlReader = new XmlTextReader(uploadStream);
xmlReader.MoveToContent();
uploadStream.Seek(position, SeekOrigin.Begin);
uploadStream.Seek(position, SeekOrigin.Current);
var streamReader = new StreamReader(uploadStream, xmlReader.Encoding);
var streamReaderString = streamReader.ReadToEnd();
return XElement.Parse(streamReaderString);
}
But that will not work. I always get EndOfStream true . But it is not!!!! -.-
For example, I have a string <test></test>. Start: 0, End: 13
If I ReadToEndor MoveToContent, then the end will succeed. EndOfStream to true .
If I reset the position through Seekor Positionequal to 0 (for example), then a new StreamReaderalways shows EndOfStream true .
The fact is that uploadStreamthis is a thread that I cannot close.
This is the SharpZipLib HTTP stream stream stream. Therefore, I cannot close this thread. I can work with him.
, Position Seek ... , ReadToEnd Position. - . !
, : -)
Advance!
:
