Image tag not closing

Topics: Developer Forum, User Forum
Apr 20, 2009 at 12:11 AM

Using the HTMLAgilityPack to write out a new image node, it seems to remove the closing tag of an image, e.g. should be but when you check outer html, has .

string strIMG = "<img src='" + imgPath + "' height='" + pubImg.Height + "px' width='" + pubImg.Width + "px' />";

HtmlNode newNode = HtmlNode.Create(strIMG);
I understand that there is no adherence to XHTML, however stripping out the /> closing tag is not desirable. Is there a way around this?
Apr 29, 2009 at 3:20 PM
Edited Apr 29, 2009 at 3:21 PM
try setting OptionWriteEmptyNodes = true;
// create doc for html parsing
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.OptionWriteEmptyNodes = true;
// load html
doc.LoadHtml( cmsPage );

This works when you are dealing with an HtmlDocument. However, if you are working with an HtmlNode directly, you may have to dig deeper and modify the source code. I was not successful using this flag independent of a parsed html document
Coordinator
May 18, 2009 at 6:55 AM

All "per-node" settings are located in a public property named ElementFlags on HtmlNode. IMG is declared by default as being empty. You can change this behavior at compile time or execution time.

May 12, 2010 at 7:44 PM

Hi-

To save people time, this seems to be how to set the IMG tag globally:

            if (HtmlNode.ElementsFlags.ContainsKey("img"))
            {
                HtmlNode.ElementsFlags["img"] = HtmlElementFlag.Closed;
            }
            else
            {
                HtmlNode.ElementsFlags.Add("img", HtmlElementFlag.Closed);
            }

I can't figure out how to make it work along with HtmlElementFlag.Empty.  This doesn't work:

            if (HtmlNode.ElementsFlags.ContainsKey("img"))
            {
                HtmlNode.ElementsFlags["img"] = HtmlElementFlag.Empty | HtmlElementFlag.Closed;
            }
            else
            {
                HtmlNode.ElementsFlags.Add("img", HtmlElementFlag.Empty | HtmlElementFlag.Closed);
            }

-Mike

 

 

May 12, 2010 at 7:46 PM

Is there any way to do this on a per-element basis?  I would rather that it didn't alter the tag's closed/not closed status if it didn't need to.

-Mike