c# - Cannot transform XML to HTML through .NET System.Xml.Xsl.XslCompiledTransform -
i have xsl:
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:cf="http://aaa" xmlns="http://aaa" exclude-result-prefixes="cf"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/cf:content"> <html> <head> <title>aaa</title> </head> <body> hello </body> </html> </xsl:template> </xsl:stylesheet>
this xml:
<?xml version="1.0" encoding="utf-8" ?> <?xml-stylesheet href="myxsl.xslt" type="text/xsl"?> <cf:content xmlns:cf="http://aaa" xmlns="http://aaa"> hello. </cf:content>
the namespace referenced xml xsd of mine (validation correct).
well, open xml file browser, xslt works.
now, have code:
string xml = "the same xml of xml file"; xslcompiledtransform transform = new xslcompiledtransform(); using (xmlreader xr = xmlreader.create("myxsl.xslt")) { transform.load(xr); } try { using (stringwriter sw = new stringwriter()) using (stringreader sr = new stringreader(xml)) using (xmlreader xr = xmlreader.create(sr)) { transform.transform(xr, new xsltargumentlist(), sw); string html = sw.tostring(); this.preview_literal.text = html; } } catch (exception ex) { throw ex; }
of course exception:
error: data @ root level invalid. line 1, position 1. - type: system.xml.xmlexception
what problem?
martin's answer:
remove default namespace, xmlns="http://aaa"
, xsl:stylesheet
html elements don't belong in namespace. example, <head>
<cf:head>
default.
Comments
Post a Comment