Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 409163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:45:47+00:00 2026-05-12T17:45:47+00:00

The following hunk of code (snipped for brevity) generates an xml doc, and spits

  • 0

The following hunk of code (snipped for brevity) generates an xml doc, and spits it out to a file. If I open the file in Visual Studio it appears to be in chinese characters. If I open it in Notepad it looks as expected. If I Console.WriteLine it look correct.

I know it’s related to encoding, but I though I had all the encoding ducks in a row. What’s missing?

StringBuilder stringBuilder = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.Unicode;
settings.Indent = true; 
settings.IndentChars = "\t";
using (XmlWriter textWriter = XmlWriter.Create(new StringWriter(stringBuilder), settings))
{
    textWriter.WriteStartElement("Submission");
    textWriter.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
    textWriter.WriteEndElement();
}

using (StreamWriter sw = new StreamWriter(new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)))
            {
                sw.Write(stringBuilder.ToString());
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:45:48+00:00Added an answer on May 12, 2026 at 5:45 pm

    The problem is that you’re writing it to disk using UTF-8, but it will claim to be UTF-16 because that’s what a StringWriter uses by default – and because you’re explicitly setting it to use Encoding.Unicode as well.

    The simplest way to fix this is to use a StringWriter which advertises itself as UTF-8:

    public class Utf8StringWriter : StringWriter
    {
        public override Encoding
        {
             get { return Encoding.UTF8; }
        }
    }
    

    … and then remove the settings.Encoding = Encoding.Unicode line. That way you’ll use UTF-8 throughout. (In fact, the Encoding property of XmlWriterSettings is ignored when you create the XmlWriter with a TextWriter anyway.)

    If you really want UTF-16, then when you create the StreamWriter, specify Encoding.Unicode there too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a file with the following changes: # Manual hunk edit mode --
Following code produces a nested array as a result for keys containing three items:
Following code takes like 2500 milliseconds on an i7-*3.4 GHz windows-7 64-bit computer to
Following code worked fine abstract class FunctionRunnable<V> implements Runnable { protected abstract V calculate();
Following chunk html code works as expected: <iframe src=http://www.amazon.com/></iframe> But when trying embed inner
Following leads in this thread and others, I've set up a code block where
following code is used to find url from a string with php. Here is
Following is my javascript code. The problem is when I call the send() function
Following example code from the libpcap documentation yields the following code which should report
Following is my code: <table border=0 align=center cellpadding=0 cellspacing=0 class=grid_table> <tr class=grid_caption><td class=grid_caption>Customer Name</td></tr>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.