StreamReader 将 MemoryStream 读取到字符串似乎会截断文件末尾
我将 HTML 文档保存到 MemoryStream,然后将该流(使用 StreamReader)读取到字符串对象。 HtmlDocument 对象已完成,但当我检查从streamReader.ReadToEnd() 分配的字符串时,文件末尾似乎已被截断。我假设我的 MemoryStream 或 StreamReader 实现有错误。有人可以帮我吗?
HtmlDocument htmlDocument = GetDocument(htmlHref);
HtmlNode scriptTag = htmlDocument.DocumentNode.SelectSingleNode("//script[@id ='HwInitialize']");
scriptTag.InnerHtml =
string.Format("org.myorg.application.init ={0};", stateJson);
MemoryStream memoryStream = new MemoryStream();
htmlDocument.Save(memoryStream); //Save Document to memory
memoryStream.Seek(0, SeekOrigin.Begin);
StreamReader streamReader = new StreamReader(memoryStream);
return streamReader.ReadToEnd(); //return the stream contents to string
I am saving an HTML document to a MemoryStream and then reading that stream (using StreamReader) out to a string object. HtmlDocument object is complete but when I inspect the string that is assigned from the streamReader.ReadToEnd() it appears that the end of the file has been truncated. I assume that my implementation of the MemoryStream or StreamReader is faulty. Can someone help me out?
HtmlDocument htmlDocument = GetDocument(htmlHref);
HtmlNode scriptTag = htmlDocument.DocumentNode.SelectSingleNode("//script[@id ='HwInitialize']");
scriptTag.InnerHtml =
string.Format("org.myorg.application.init ={0};", stateJson);
MemoryStream memoryStream = new MemoryStream();
htmlDocument.Save(memoryStream); //Save Document to memory
memoryStream.Seek(0, SeekOrigin.Begin);
StreamReader streamReader = new StreamReader(memoryStream);
return streamReader.ReadToEnd(); //return the stream contents to string
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
htmlDocument.DocumentNode.OuterHtml 属性会将您的 htmlDocument(包括您的任何更改)序列化为 html 字符串。
The htmlDocument.DocumentNode.OuterHtml property will serialize your htmlDocument, including any of your changes, into a html string.