简体   繁体   中英

memorystream not saving all content

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 Save overload which uses a TextWriter :

HtmlDocument email = new HtmlDocument();
using (var sw = new StringWriter())
{
    email.Save(sw);
    return sw.ToString();
}

Note that you should always dispose objects which implement IDisposable (the simplest way is to wrap them inside using blocks as shown here).

My suspicion is that you are getting the whole string back here, and that are not Flush() ing a stream somewhere else- probably when you save it to disk. The code here looks fine.

If you post the function where you save it, my guess is that we'll see the problem there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM