Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Generate an xml file with StringWriter(UTF-8) ??

Generate an xml file with StringWriter(UTF-8) ??

Scheduled Pinned Locked Moved C#
databasexmlhelpcsharpgraphics
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zoltix
    wrote on last edited by
    #1

    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

    S 1 Reply Last reply
    0
    • Z zoltix

      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

      S Offline
      S Offline
      Stephane Rodriguez
      wrote on last edited by
      #2

      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 );

      Z 1 Reply Last reply
      0
      • S Stephane Rodriguez

        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 );

        Z Offline
        Z Offline
        zoltix
        wrote on last edited by
        #3

        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=-

        S 1 Reply Last reply
        0
        • Z zoltix

          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=-

          S Offline
          S Offline
          Stephane Rodriguez
          wrote on last edited by
          #4

          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. ;)

          Z 1 Reply Last reply
          0
          • S Stephane Rodriguez

            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. ;)

            Z Offline
            Z Offline
            zoltix
            wrote on last edited by
            #5

            You are great ........ Thanks a lot ......:-D -=zoltx=-

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups