1

hi here i am generating hex 01 and hex 03 with xslt and using c# on transformation hex 01 was genarating like a space in text document if i see in hexadecimal format i could see

 some junk is generating before the hex 01, note this issue was not replicating for hex 03 it was working good,exactly etx was generating, how could i solve this issue ? any idea please..

 <?xml version="1.0" encoding="utf-8"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"               xmlns:myScripts="myScripts">
<xsl:output method="text" indent="yes"/>

<msxsl:script implements-prefix="myScripts" language="C#">
public string SOH()
{
  return '\u0001'.ToString();
}

  </msxsl:script>
 <msxsl:script implements-prefix="myScripts" language="C#">
   public string ETX()
{
  return '\u0003'.ToString();
}
</msxsl:script>

  <xsl:template match="/">
    <xsl:value-of select="myScripts:SOH()"></xsl:value-of>
    <xsl:value-of select="myScripts:ETX()"></xsl:value-of>
 </xsl:template>

</xsl:stylesheet>

here is my c# code behind code :

 XslCompiledTransform transform = new XslCompiledTransform(true);
                        transform.Load(strCTD, new XsltSettings() { EnableScript = true }, null); // Loading the given Xslt document
                        var writerSettigns = transform.OutputSettings.Clone();
                        writerSettigns.CheckCharacters = false;
       string strFileName = strpath + langid + strCSVFILE + strMsgType + strORGMSG +  strSeqNum + strNowDate + strHour + strMin + strSec + ".FTS";

                        try
                        {

                            MemoryStream memoryStream = new MemoryStream();
                            XmlWriterSettings xmlWriterSettings = new  XmlWriterSettings();
                            xmlWriterSettings.Encoding = new UTF8Encoding(false);
                            xmlWriterSettings.ConformanceLevel = ConformanceLevel.Document;
                            xmlWriterSettings.Indent = true;

                            using (XmlWriter writer = XmlWriter.Create(strFileName, writerSettigns)) 
                            {
                                transform.Transform("sampleCTD.xml", xmlArgsList, writer);
                            } 

                 string xmlString = Encoding.UTF8.GetString(memoryStream.ToArray());

               System.IO.StreamWriter file = new System.IO.StreamWriter(strFileName);
                            file.WriteLine(xmlString);

                            file.Close();
                      }
                      catch (Exception ex)
                       {
                     LogError(1, "Form1", "FileGenerationForCID",    ex.Message.ToString(), ex.StackTrace.ToString());
                        throw ex;
                    }

i have added some code to to avoid BOM error on starting of the text, even though it was not sorted out kindly please can any one suggest on this to me soon ....

shannu
  • 187
  • 1
  • 2
  • 11
  • how to avoid Byte order mark error,actually it was not a junk it is a bom error how to avoid ? – shannu Feb 08 '12 at 11:40

3 Answers3

1

What you're seeing is the UTF-8 BOM, the Byte Order Mark, that allows UTF-8 aware readers to understand the encoding correctly. Perhaps change your output declaration to:

<xsl:output method="text" indent="yes" encoding="unicode"/>
Avner Shahar-Kashtan
  • 14,492
  • 3
  • 37
  • 63
  • @Anver Shahar-Kashtan the below you have edited snippet like to remove tostring(); i have tested it throws a error like convert char to string, but your guess was corrected byte order mark EF BB BF these three are coming before 01,this should to be avioded – shannu Feb 08 '12 at 10:59
1

Use an entity instead of outputting the raw character. This takes the form of something like &#1; or &#3;.

Zenexer
  • 18,788
  • 9
  • 71
  • 77
  • Then you need to focus on fixing that issue. Entities are what you should be using. – Zenexer Feb 08 '12 at 10:03
  • 1
    On using entities: http://stackoverflow.com/questions/2243823/xsl-include-some-ascii-control-chars-when-method-text – GSerg Feb 08 '12 at 10:17
  • According to that, @shannu, an entity is your only option, if I understand correctly. – Zenexer Feb 09 '12 at 04:19
0

And on another topic - I don't understand your custom script methods. My first instinct was "Why not just use return "\u0001"; instead of putting a char and converting it to string, but when I think about it, you can jump use &#0001 directly embedded in the XML.

Either use XML entities directly in your XSLT, like this:

<xsl:value-of select="&#0001;"/>

or, if for some reason you can't embed the entities, make the code snippets simpler, without casting to string;

public string SOH ()
{  
   return "\u0001";
}
Avner Shahar-Kashtan
  • 14,492
  • 3
  • 37
  • 63
  • i dint understand what your trying to say, can you please briefly – shannu Feb 08 '12 at 10:01
  • anver here soh is not visible in text document but i could see in hexa edit of the file then it was showing as 01, why it was happening ? – shannu Feb 08 '12 at 12:48
  • The SOH or the three BOM characters? That's because most text editors recognize the BOM and don't display it, while a hex editor displays the raw bytes in the file. – Avner Shahar-Kashtan Feb 08 '12 at 13:02
  • start of the message i could see some space,if i see in hexadecimal format of that file it was displaying in bom three characters and 01 , normally in text document soh looks like small r and etx looks like small l in the file but it was generating as a space – shannu Feb 08 '12 at 13:16