This project has moved. For the latest updates, please go here.

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();


                StreamReader streamReader = new StreamReader(htmlStream);

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

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


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

Your expedient assistance is greatly appreciated.

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.