Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Re: Question Marks (???) in out put HTMl >Thread Next - Re: Question Marks (???) in out put HTMl Re: Question Marks (???) in out put HTMlTo: 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/
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
