Reputation: 45
I had asked a question about how to investigate the contents of XMLWriter
object while debugging.
I am trying to check the contents of an XmlReader
object that is created from a memory stream in a similar way as given in the answer of the linked question. But I am getting UnauthorizedAccessException
stating MemoryStream's internal buffer cannot be accessed.
How to verify the xml read from the MemoryStream?
public PInfo(byte[] pBytes)
{
MemoryStream pMem = new MemoryStream(pBytes);
XmlReader reader = XmlReader.Create(pMem);
//MemoryStream's internal buffer cannot be accessed.
string s = Encoding.UTF8.GetString(pMem.GetBuffer(), 0, (int)pMem.Position);
....
}
Upvotes: 2
Views: 350
Reputation: 11214
Check out the MSDN Docs for the particular constructor you're using, MemoryStream(Byte[])
. When you instantiate it this way, GetBuffer()
will throw that exception, since the buffer is not actually visible. You should instead use this constructor, and be sure to set publiclyVisible
to true
.
Upvotes: 3