Saving HTML as XHTML in .NET 6

87 views Asked by At

I need to convert a html File to XHTML in C# .NET 6.

Is there any simple way to do it?

What i tried so far was using HtmlAgilityPack and set OptionOutputAsXml = true like so:

var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html); // Load HTML string here
htmlDoc.OptionOutputAsXml = true; // Set output as XML

using var outStream = new FileStream(savePath, FileMode.Create);
htmlDoc.Save(outStream, Encoding.UTF8);

But i wonder if the output i really correct XHTML as i looks like this:

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>
      //
      <![CDATA[

        XHTML Document
      
//]]>//
    </title>
    <style type="text/css">
      //<![CDATA[

      @media print {

        body,
        .page {
          margin: 0;
          box-shadow: 0;
        }
      }
    //]]>//
    </style>
    ...

The CDATA just doesnt seems right as first of all there are those // infront and second I have some reference XHTML Documents on which I orientate myself, containig css and none of them has it declared with CDATA.

So is there any free alternative?

0

There are 0 answers