Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Question Marks (???) in out put HTMl

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 3/7/2009 1:45:00 PM
Zest4Quest wrote:

>   I have the following function in C# which transforms the XML.
> The GenerateXML() returns a string that represents the xml...
> 
>  public class XmlGenerator<T> where T : class
>     {
>         private T _dataContract;
>         public XmlGenerator(T dataContract)
>         {
>             _dataContract = dataContract;
>         }
> 
>         /// <summary>
>         /// Generates the XML from the Object
>         /// </summary>
>         /// <returns></returns>
>         private string GenerateXml()
>         {
>             UnicodeEncoding uniEncoding = new UnicodeEncoding();
>             string buffer;
> 
>             DataContractSerializer ser = new 
> DataContractSerializer(typeof(T));
> 
>             StreamWriter stWriter = null;
>             MemoryStream memoryStream = new MemoryStream();
>             XmlTextWriter xmlWriter = new XmlTextWriter(memoryStream, 
> Encoding.Unicode);
> 
>             stWriter = new StreamWriter(memoryStream);
>             ser.WriteObject(memoryStream, this._dataContract);
> 
>             buffer = Encoding.ASCII.GetString(memoryStream.GetBuffer());
> 
>             return 
> buffer.Replace(@"xmlns=""http://schemas.datacontract.org/2004/07/EDIM.DataContracts""", "");
>         }
> 
>         public string TransformXmlFinal(string xSLTFileName)
>         {
>              
>             XmlDataDocument d = new XmlDataDocument();
>             string sXML = this.GenerateXml();
>             d.LoadXml(this.GenerateXml());
> 
>             XslCompiledTransform xslTransform = new XslCompiledTransform();
> 
>             xslTransform.Load(xSLTFileName);
> 
>             MemoryStream memoryStreamOutPut = new MemoryStream();
>             xslTransform.Transform(d, null, memoryStreamOutPut);
>             return Encoding.ASCII.GetString(memoryStreamOutPut.GetBuffer());

Don't use all those MemoryStreams, if you want a string then you can use 
a StringWriter, there is no need to mess with MemoryStreams, Encodings, 
byte arrays:



         private string GenerateXml()
         {
             string generatedXml;

             DataContractSerializer ser = new
DataContractSerializer(typeof(T));

             using (StringWriter sw = new StringWriter())
             {
                 using (XmlWriter xw = XmlWriter.Create(sw))
                 {
                     ser.WriteObject(xw, this._dataContract);
                     xw.Close();
                 }
                 generatedXml = sw.ToString();
             }
             return generatedXml;

         }

         public string TransformXmlFinal(string xSLTFileName)
         {

             string sXML = this.GenerateXml();

             XslCompiledTransform xslTransform = new XslCompiledTransform();

             xslTransform.Load(xSLTFileName);

             StringWriter result = new StringWriter();

             xslTransform.Transform(new XPathDocument(new 
StringReader(sXML)), null, result);

             return result.ToString();

         }

That way I hope the question marks should no longer appear as I suspect 
they occured by using Encoding.ASCII to decode a byte array in a 
different encoding and perhaps containing a byte order mark.

-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent