HTML and MemoryStream

Topics: Developer Forum, User Forum
Feb 6, 2015 at 6:19 PM
Can anyone find a reason for this code not working? That is, for the stream returning empty?
IList<QuestionDefinitions> questiondefs = new List<QuestionDefinitions>();

                QuestionDefinitions questdef = new QuestionDefinitions();
                questdef.OriginalTxt = Server.HtmlEncode(@"<dd> [] text...text...text <dd> [] text...text");

                HtmlDocument doc = new HtmlDocument();
                //doc.OptionFixNestedTags = true;
                //doc.OptionWriteEmptyNodes = true;
                doc.OptionDefaultStreamEncoding = Encoding.UTF8;

                doc.LoadHtml(@"<table><tr><td><dd>text...text...text <dd>text...text</td></tr></table>");

                Stream htmlStream = new MemoryStream();

                doc.Save(htmlStream);

                StreamReader streamReader = new StreamReader(htmlStream);

                questdef.NewTxt = streamReader.ReadToEnd().Trim();

                questdef.IsSame = questdef.OriginalTxt == questdef.NewTxt ? true : false;

                questiondefs.Add(questdef);

                this.lblCount.Text = questiondefs.Count.ToString();
                dgQuestionDefs.DataSource = questiondefs;
                dgQuestionDefs.DataBind();
I tried including moving the position of the stream to 0, but that had no effect.

Your expedient assistance is greatly appreciated.

Thanks...Chris
Feb 19, 2015 at 6:56 PM
you're initializing a new stream but not loading anything into it.

Stream htmlStream = new MemoryStream();

so when you call questdef.NewTxt = streamReader.ReadToEnd().Trim(); the stream is empty
Feb 19, 2015 at 7:14 PM
Doesn't the next line save the contents of the HTMLDocument into "htmlStream"?

Right now, I am just looking for an answer for my own knowledge, as I have already resolved my bigger issue, using an alternative method.

Thanks...Chris