Loading XML into a memorystream
SO I am trying to get a simple dataset from the strInstallDataSet
str开发者_如何学运维ing into the dataset, using the code below, when I have the debugger connected I can see that strInstallDataSet
has data, byteArray
has data, but even after reading msDataset
has nothing, length just sits at 0, I have tried setting the position before and after reading but it still just doesn't pick up any data. Any ideas?
MemoryStream msDataset = new MemoryStream();
if (strInstallDataSet != null)
{
// Convert string to byte array.
byte[] byteArray = Encoding.ASCII.GetBytes(strInstallDataSet);
msDataset.Read(byteArray, 0, byteArray.Length);
// Put stream back into dataset object.
dsInstallData.ReadXml(msDataset);
msDataset.Close();
msDataset.Dispose();
}
You probably want to be doing the following:
using(StringReader reader = new StringReader(strInstallDataSet))
{
dsInstallData.ReadXml(reader);
}
You are not writing anything to the stream, only reading msDataset.Read
...
Side note 1: you are using very low level methods - there are Reader/Writer classes that will correctly take care of encoding already.
Side note 2: use "using" instead of manually calling Close or Dispose (and don't call 2 of them together as both do exactly the same things).
You're misunderstanding what the MemoryStream.Read()
does, it reads into the byte array, not into the memorystream.
You want MemoryStream.Write()
where you have MemoryStream.Read()
Or better yet...
MemoryStream xmlMemoryStream = new MemoryStream(byteArray);
You must use Write method instead of read. I think you want to write your bytearray into your memory stream.
精彩评论