onizet / html2openxml

Html2OpenXml is a small .Net library that convert simple or advanced HTML to plain OpenXml components. This program has started in 2009, initially to convert user's comments from SharePoint to Word.
MIT License
297 stars 106 forks source link

Exception in code HtmlConverter.ProcessTag.cs:line 281 #111

Closed TreF555 closed 5 months ago

TreF555 commented 2 years ago

Error "Sequence contains no elements"

at System.Linq.ThrowHelper.ThrowNoElementsException() at System.Linq.Enumerable.First[TSource](IEnumerable`1 source) at HtmlToOpenXml.HtmlConverter.ProcessHeading(HtmlEnumerator en) in ...\Html2OpenXml\HtmlConverter.ProcessTag.cs:line 281 at HtmlToOpenXml.HtmlConverter.ProcessHtmlChunks(HtmlEnumerator en, String endTag) in ...\Html2OpenXml\HtmlConverter.cs:line 208 at HtmlToOpenXml.HtmlConverter.Parse(String html) in ...\Html2OpenXml\HtmlConverter.cs:line 105

// Check if the line starts with a number format (1., 1.1., 1.1.1.)
// If it does, make sure we make the heading a numbered item
OpenXmlElement firstElement = elements.First(); <== Exception  "Sequence contains no elements"

elements.count == 0 !

antoinebidault commented 2 years ago

It happens when you have an empty heading in the html string like <h1></h1>. A quick workaround is to clear the empty headings from HTML before conversion :

private string RemoveEmptyHeadings(string htmlString)
{
    for (int i =1; i < 7; i++)
    {
        htmlString = htmlString.Replace($"<h{i}></h{i}>", "");
    }
    return htmlString;
}
onizet commented 5 months ago

This is fixed in 2.4.1