- For individual users
- Instant access to solutions
- Ask your tech questions
- Start your 30-day Free Trial
Main Topics
Browse All TopicsWhen 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.U
Don’t work, I am receive a error message “C:\Inetpub\wwwroot\Licenc
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="GenerateXmlTre
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>
/// Summary description for GenerateXmlTreeView.
/// </summary>
public class GenerateXmlTreeView : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
//StringReader = new StreamReader("site/Generat
string strSelect;
// Put user code to initialize the page here
if( Request.QueryString["selec
{
strSelect =Request.QueryString["sele
}else {
strSelect= "";
}
string strIdCompany;
if( Request.QueryString["IdCom
{
strIdCompany =Request.QueryString["IdCo
}
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_fil
//Xml Generate
ArrayContainer Level = new ArrayContainer(100);
StringWriter s = new StringWriter();
//string test = s.Encoding.ToString();
XmlTextWriter tw = new XmlTextWriter(s);// ,Encoding.UTF8
tw.Formatting=Formatting.I
tw.WriteStartDocument();
tw.WriteStartElement("tree
tw.WriteAttributeString("t
tw.WriteStartElement("cust
tw.WriteStartElement("para
tw.WriteAttributeString("n
tw.WriteAttributeString("v
tw.WriteEndElement();
tw.WriteStartElement("para
tw.WriteAttributeString("n
tw.WriteAttributeString("i
tw.WriteEndElement();
tw.WriteEndElement();
int level=0;
string IdElement;
Level.add("");
string Name;
string IdPrduit;
string IdParent;
string License;
string PrevIdParent="";
string PrevIdElement="";
bool FlagElementFirst= true ;
foreach(DataRow r in DT.Rows){
if (FlagElementFirst == true)
{
Level.Remove(0);
Level.add(r["idParent"].To
}
IdElement = r["id"].ToString();
Name = r["Name"].ToString();
IdPrduit = r["IdPrduit"].ToString();
IdParent = r["idParent"].ToString();
License = r["license"].ToString();
if(( Level[IdParent]>=level ||level==0 ))
{
if((IdParent=="" || IdParent!=PrevIdParent ) && (IdPrduit=="") )
{
Console.WriteLine("Name");
tw.WriteStartElement("fold
tw.WriteAttributeString("t
if (FlagElementFirst == true)
{
tw.WriteAttributeString("i
}
else
{
tw.WriteAttributeString("i
}
level++ ;
Level.add(IdElement);
PrevIdParent=IdParent;
PrevIdElement =IdElement;
}
else if((IdParent==PrevIdParent
{
}
else if(IdPrduit!="")
{
string reg;
if(License=="Y") {reg="true";}
else {reg="false" ; }
tw.WriteStartElement("leaf
tw.WriteAttributeString("t
tw.WriteAttributeString("u
tw.WriteAttributeString("r
tw.WriteEndElement();
//tw.WriteAttributeString(
}
}
else
{
while(level>Level[IdParent
{
tw.WriteEndElement();
Level.Remove(level);
level--;
}
if(IdPrduit !="")
{
//tw.WriteElementString(Na
string reg;
if(License=="Y") {reg="true";}
else {reg="false" ; }
tw.WriteStartElement("leaf
tw.WriteAttributeString("t
tw.WriteAttributeString("u
tw.WriteAttributeString("r
tw.WriteEndElement();
}
else
{
tw.WriteStartElement("fold
tw.WriteAttributeString("t
tw.WriteAttributeString("i
Level.add(IdElement);
PrevIdParent=IdParent;
PrevIdElement =IdElement;
level++;
}
}
FlagElementFirst = false;
}
tw.WriteEndDocument();
tw.Flush();
tw.Close();
Response.Clear();
Response.ContentEncoding = Encoding.UTF8;//
//Encoding.GetEncoding(125
Response.ContentType="text
Response.Write(s.ToString(
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.P
}
#endregion
}
}
This question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership