Generate an xml file with StringWriter(UTF-8) ??
-
When I try to generate an xml file with StringWriter, I can’t change the encoding (UTF-16) to UTF-8. This application, having to have a format xml(utf-8) To preserve compatibility with other programs. Ex StringWriter s = new StringWriter(); XmlTextWriter tw = new XmlTextWriter(s,Encoding.UTF-8); Don’t work, I am receive a error message “C:\Inetpub\wwwroot\LicenceTools\site\GenerateXmlTreeView.aspx.cs(58): Argument '1': cannot convert from 'System.IO.StringWriter' to 'System.IO.Stream'” I would like having "?xml version="1.0" encoding="utf-8"?" rather than "?xml version="1.0" encoding="utf-16"?" Could you help me? Thanks in advance PS: It s a stream for WebServer. Application Code: Htm: <%@ Page language="c#" Codebehind="GenerateXmlTreeView.aspx.cs" AutoEventWireup="false" Inherits="LicenceTools.site.GenerateXmlTreeView" %> C#: using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using ZZDataFromDb.ZZConn; using ZZTools.ZZTools; using System.Xml; using System.IO; using System.Globalization; using System.Text; using System.Xml.XPath; namespace LicenceTools.site { /// /// Summary description for GenerateXmlTreeView. /// public class GenerateXmlTreeView : System.Web.UI.Page { private void Page_Load(object sender, System.EventArgs e) { //StringReader = new StreamReader("site/GenerateXmlTreeView.aspx"); string strSelect; // Put user code to initialize the page here if( Request.QueryString["select"] != null) { strSelect =Request.QueryString["select"]; }else { strSelect= ""; } string strIdCompany; if( Request.QueryString["IdCompany"] != null) { strIdCompany =Request.QueryString["IdCompany"]; } else { strIdCompany= "%"; } DataContainer dc=new DataContainer(); // create a data container for this function dc.Set("select", strSelect); // add the user name dc.Set("IdCompany", strIdCompany); // add the user name DBData dts=new DBData(dc); // create a interface to the DB technology DataTable DT; DT = dts.QueryMultiRow("sql_fillTreeView"); // query if the account exists. Always returns a value //Xml Generate ArrayContainer Level = new ArrayContainer(100); StringWriter s = new StringWrit
-
When I try to generate an xml file with StringWriter, I can’t change the encoding (UTF-16) to UTF-8. This application, having to have a format xml(utf-8) To preserve compatibility with other programs. Ex StringWriter s = new StringWriter(); XmlTextWriter tw = new XmlTextWriter(s,Encoding.UTF-8); Don’t work, I am receive a error message “C:\Inetpub\wwwroot\LicenceTools\site\GenerateXmlTreeView.aspx.cs(58): Argument '1': cannot convert from 'System.IO.StringWriter' to 'System.IO.Stream'” I would like having "?xml version="1.0" encoding="utf-8"?" rather than "?xml version="1.0" encoding="utf-16"?" Could you help me? Thanks in advance PS: It s a stream for WebServer. Application Code: Htm: <%@ Page language="c#" Codebehind="GenerateXmlTreeView.aspx.cs" AutoEventWireup="false" Inherits="LicenceTools.site.GenerateXmlTreeView" %> C#: using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using ZZDataFromDb.ZZConn; using ZZTools.ZZTools; using System.Xml; using System.IO; using System.Globalization; using System.Text; using System.Xml.XPath; namespace LicenceTools.site { /// /// Summary description for GenerateXmlTreeView. /// public class GenerateXmlTreeView : System.Web.UI.Page { private void Page_Load(object sender, System.EventArgs e) { //StringReader = new StreamReader("site/GenerateXmlTreeView.aspx"); string strSelect; // Put user code to initialize the page here if( Request.QueryString["select"] != null) { strSelect =Request.QueryString["select"]; }else { strSelect= ""; } string strIdCompany; if( Request.QueryString["IdCompany"] != null) { strIdCompany =Request.QueryString["IdCompany"]; } else { strIdCompany= "%"; } DataContainer dc=new DataContainer(); // create a data container for this function dc.Set("select", strSelect); // add the user name dc.Set("IdCompany", strIdCompany); // add the user name DBData dts=new DBData(dc); // create a interface to the DB technology DataTable DT; DT = dts.QueryMultiRow("sql_fillTreeView"); // query if the account exists. Always returns a value //Xml Generate ArrayContainer Level = new ArrayContainer(100); StringWriter s = new StringWrit
using System.IO;
using System.Xml;MemoryStream ms = new MemoryStream();
XmlTextWriter tw = new XmlTextWriter(ms,new System.Text.UTF8Encoding());// -- your xml stuff begins here --
tw.WriteStartDocument();
tw.WriteStartElement("...");
tw.WriteEndElement();
tw.WriteEndDocument();
tw.Flush();
tw.Close();
// -- your xml stuff ends here// convert byte[] to String
String s = System.Text.Encoding.UTF8.GetString( ms.GetBuffer() );
MessageBox.Show( s ); -
using System.IO;
using System.Xml;MemoryStream ms = new MemoryStream();
XmlTextWriter tw = new XmlTextWriter(ms,new System.Text.UTF8Encoding());// -- your xml stuff begins here --
tw.WriteStartDocument();
tw.WriteStartElement("...");
tw.WriteEndElement();
tw.WriteEndDocument();
tw.Flush();
tw.Close();
// -- your xml stuff ends here// convert byte[] to String
String s = System.Text.Encoding.UTF8.GetString( ms.GetBuffer() );
MessageBox.Show( s );Thanks a lot. It works..... somebody, send me an others solution which work too public class StringWriterWithEncoding : StringWriter { Encoding encoding; public StringWriterWithEncoding (Encoding encoding) { this.encoding = encoding; } public override Encoding Encoding { get { return encoding; } } } Then you'd do: XmlTextWriter = new XmlTextWriter (new StringWriterWithEncoding (Encoding.UTF8)); -=zoltx=-
-
Thanks a lot. It works..... somebody, send me an others solution which work too public class StringWriterWithEncoding : StringWriter { Encoding encoding; public StringWriterWithEncoding (Encoding encoding) { this.encoding = encoding; } public override Encoding Encoding { get { return encoding; } } } Then you'd do: XmlTextWriter = new XmlTextWriter (new StringWriterWithEncoding (Encoding.UTF8)); -=zoltx=-
I have to admit the StringWriter derived class is a much better approach to the encoding issue. Use it instead of the C-like memory streams I have suggested. ;)
-
I have to admit the StringWriter derived class is a much better approach to the encoding issue. Use it instead of the C-like memory streams I have suggested. ;)