"Illegal character in path" using XSLT

4.1k Views Asked by At

I'm getting an "Illegal character in path" exception when I call the Transform method of the XslCompiledTransform class.

Here is my code :

// Maybe there is a problem in this path 
string xsltPath = @"..\..\HtmlAttributesParser.xslt";

XslCompiledTransform xsltCompiled = new XslCompiledTransform();
xsltCompiled.Load(xsltPath, new XsltSettings(false, true), new XmlUrlResolver());
StringBuilder output = new StringBuilder();

xsltCompiled.Transform(content, XmlWriter.Create(output));

There isn't any * ? " < > | in my path so I wonder why I get this Exception.

Regarding to the Exception message, there is no connection between the value of my content var and this exception right ?

Edit : Here is the content which works on an online XSLT tester

<div class="pk-link">
<a href="/STORE/Pages/myPage.aspx" url="/STORE/Pages/myPage.aspx" width="" heigth=""  target="_blank">
    <img border="0" src="/link_download.gif"/>
      Download
</a>
</div>
1

There are 1 best solutions below

2
On BEST ANSWER

If you have a string with XML input then you need to use an overload of the Transform method expecting an XmlReader or XPathDocument (or more generally IXPathNavigable) created over that string so do e.g.

string result;

using (StringReader sr = new StringReader(content))
{
  using (XmlReader xr = XmlReader.Create(sr))
  {
    using (StringWriter sw = new StringWriter())
    {

       xsltCompiled.Transform(xr, null, sw);

      result = sw.ToString();
    }
  }
}

The overloads of the Transform method that take a string as the first argument expect a file path or URL, not the XML input.