Aspnetcore: .NET Core Version of System.Web.UI.HtmlTextWriter

Created on 5 Oct 2018  路  9Comments  路  Source: dotnet/aspnetcore

Following on from https://github.com/dotnet/corefx/issues/24169. What is the alternative? I'm using this type to do XSL transforms like so:

```c#
private static Stream XslTransform(XDocument document, XmlReader xslReader)
{
var xslTransform = new XslCompiledTransform();
var stringBuilder = new StringBuilder();

using (var stringWriter = new StringWriter(stringBuilder))
{
    using (var htmlWriter = new HtmlTextWriter(stringWriter))
    {
        xslTransform.Load(xslReader);
        xslTransform.Transform(document.CreateReader(), new XsltArgumentList(), htmlWriter);
        htmlWriter.Flush();
    }
}

return new MemoryStream(Encoding.UTF8.GetBytes(stringBuilder.ToString()));

}
```

Most helpful comment

I agree with op, this is a need. XMlTextWriter closes tags that it sholdn't IE, Edge, Chrome puke on those tags.

All 9 comments

@rynowak ?

@RehanSaeed in this scenario, what does using an HtmlTextWriter provider? Can you just use a plain TextWriter and get the same results? That is, just call:

c# xslTransform.Transform(document.CreateReader(), new XsltArgumentList(), stringWriter);

One of the things it provides is the ability to output HTML5 (<br>) instead of XHTML (<br/>) which is what you get if you use XmlTextWriter.

Does it really do that as part of a transformation? I thought HtmlTextWriter just added a few additional features on top of the regular TextWriter, such as some extra APIs (for doing HTML-ish stuff), and then adding indentation support to existing APIs.

It certainly does. There is a list of HTML tags and whether they are closing or non-closing here.

My use case and probably the most common use case is XSLT transformation via HTML text writer, so in this scenario I don't use any of the extra API's that let you manually write HTML tags.

Hmm, odd, I just don't see how that code is used in the XSLT scenario. As far as I recall (and from a brief look at the code), those mapping are used only when someone is calling the HtmlTextWriter-specific APIs, such as RenderBeginTag. Does the XSLT transform downcast the writer and call HTML-specific APIs?

Here is some relevant sample code:

        public string Translate(XDocument document)
        {
            using (var stream = this.GetXsltStream())
            {
                using (var xslReader = XmlReader.Create(stream))
                {
                    return Transform(document, xslReader);
                }
            }
        }

        private static string Transform(XDocument document, XmlReader xslReader)
        {
            var xslTransform = new XslCompiledTransform();
            var stringBuilder = new StringBuilder();

            using (var stringWriter = new StringWriter(stringBuilder))
            {
                using (var htmlWriter = new HtmlTextWriter(stringWriter))
                {
                    xslTransform.Load(xslReader);
                    xslTransform.Transform(document.CreateReader(), new XsltArgumentList(), htmlWriter);
                    htmlWriter.Flush();
                }
            }

            return stringBuilder.ToString();
        }

We're closing this issue because it is not planned for any upcoming release.

I agree with op, this is a need. XMlTextWriter closes tags that it sholdn't IE, Edge, Chrome puke on those tags.

Was this page helpful?
0 / 5 - 0 ratings