html2openxml
html2openxml copied to clipboard
Exception in code HtmlConverter.ProcessTag.cs:line 281
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 !
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;
}
This is fixed in 2.4.1