Got a big issue with memorystream in c#. My application gets the html of an email, and parses the src values of the images that are attachments and change those src values to another ones. That works perfectly fine. Problem is i have to save the resultant html in a sharepoint list. and it does, but when i see the content in the sharepoint list, it doesnt show the email, just some part of it. I dont know if the memory stream is not saving the content at all, or if the string am saving the result doesnt have enough capacity for saving what the memory stream is storing. If anyone has any ideas please post them!
string SRC = "";
int indice = 0;
//Console.WriteLine(body);
HtmlDocument email = new HtmlDocument();
email.LoadHtml(body);
Console.WriteLine("bodylength: " + body.Length);//original length
foreach (HtmlNode img in email.DocumentNode.SelectNodes("//img"))
{
SRC = img.GetAttributeValue("src", null);
for (int i = 0; i < contentIDS.Count; i++)
{
if (SRC.Equals(contentIDS[i].ToString()))
{
//Console.WriteLine("contents" + contentIDS[i].ToString());
indice = i;
break;
}
}
img.SetAttributeValue("src", urls[indice].ToString());
Console.WriteLine(img.GetAttributeValue("src", null));
}
//se guarda en memoria los cambios hechos en el html y se retorna e tipo string el html con los cambios realizados
MemoryStream memoryStream = new MemoryStream();
email.Save(memoryStream);
//memoryStream.SetLength(body.Length);
memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
StreamReader streamReader = new StreamReader(memoryStream);
return streamReader.ReadToEnd();//this is then store in a string when i call this method. The lenght of that string is much much smaller than the original one.
You can simplify this by using a
Saveoverload which uses aTextWriter:Note that you should always dispose objects which implement
IDisposable(the simplest way is to wrap them insideusingblocks as shown here).