ib11
ib11

Reputation: 2558

XML Illegal Characters in path using XPathDocument to do XSL Transform in C#

I have an XML in a string that I need to actually transform to html using an xsl.

I do the transform with XslCompiledTransform. In order for this to work, I am parsing the string that contains the XML to XML using XPathDocument.

However if I try to parse the string straight to the XPathDocument, then I get the error:

Illegal Characters in path.

So I had to include a StringReader in order to be able to parse the string to the XPathDocument. (Using the solutions in the posts I linked below.)

Here is my step by step procedure:

  1. The string is retrieved from SDL Trados Studio and it depends on the XML that is being worked on (how it was originally created and loaded for translations) the string sometimes has a BOM sometimes not. The 'xml' is actually parsed from the segments of the source and target text and the structure element. The textual elements are escaped for xml and the markup and text is joined in one string. (My separate post on the removal of the BOM is C# XPathDocument parsing string to XML with BOM.)

  2. The the string is then parsed into an XPathDocument using a StringReader.

  3. The transform is done with the XslCompiledTransform, using a StringBuilder and a StringWriter.

  4. Transformed xml (now html) is saved to a file.

Here is my code:

//Recreate XML file using an extractor returns a string array
string strSourceXML = String.Join("", extractor.TextSrc);

//strip BOM
strSourceXML = strSourceXML.Substring(strSourceXML.IndexOf("<?"));

//Transform XML with the preview XSL
var xSourceDoc = new XPathDocument(strSourceXML);

//Load XSL
var xTr = new XslCompiledTransform();
var xslt = Settings.GetValue("WordPreview", "XSLTpath", "");
xTr.Load(xslt);

//Parse XML string
dynamic xSourceDoc;
using (StringReader s = new StringReader(strSourceXML))
{
    xSourceDoc = new XPathDocument(s);
}

//Transform the XML
StringBuilder sb1 = new StringBuilder();
StringWriter swSource = new StringWriter(sb1);
xTr.Transform(xSourceDoc, null, swSource);

//Transformed file saved to the disk
string tmpSourceDoc = Path.GetTempFileName();

System.IO.StreamWriter writer1 = new System.IO.StreamWriter(tmpSourceDoc, false, Encoding.Unicode);
writer1.Write(sb1.ToString());
writer1.Close();

My question is: Is there a simpler way to solve it? Any suggestions to transform the string straight using the XSLT? Or if not, is there a direct way to parse a string to the XPathDocument?

I have searched over many posts on Stack Overflow such as these:

But none of them give me the solution to do this simpler. Any suggestion is welcome. Thanks.

Upvotes: 1

Views: 1079

Answers (1)

Alexander Petrov
Alexander Petrov

Reputation: 14231

Don't need the intermediate StringBuilder and StringWriter.
XsltCompiledTransform instance can immediately writes to the stream on disk.

string strSourceXML = string.Concat(extractor.TextSrc);

strSourceXML = strSourceXML.Substring(strSourceXML.IndexOf("<?"));

var xTr = new XslCompiledTransform();
var xslt = Settings.GetValue("WordPreview", "XSLTpath", "");
xTr.Load(xslt);

string tmpSourceDoc = Path.GetTempFileName();

using (var reader = new StringReader(strSourceXML))
using (var writer = new StreamWriter(tmpSourceDoc, false, Encoding.Unicode))
{
    var xSourceDoc = new XPathDocument(reader);
    xTr.Transform(xSourceDoc, null, writer);
}

Upvotes: 1

Related Questions