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:
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) thestring
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 onestring
. (My separate post on the removal of the BOM is C# XPathDocument parsing string to XML with BOM.)The the
string
is then parsed into anXPathDocument
using aStringReader
.The transform is done with the
XslCompiledTransform
, using aStringBuilder
and aStringWriter
.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.