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

  • Home
  • SEARCH
  • 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 428121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:38:39+00:00 2026-05-12T19:38:39+00:00

I have this code to create an ATOM feed Dim xmlResult As New StringBuilder

  • 0

I have this code to create an ATOM feed

Dim xmlResult As New StringBuilder
Dim settings As New XmlWriterSettings
Dim atomWriter As XmlWriter = XmlWriter.Create(xmlResult, settings)
Dim atomFormatter As Atom10FeedFormatter = New Atom10FeedFormatter(feed)
atomFormatter.WriteTo(atomWriter)
atomWriter.Close()

It returns XML that starts like this:

<?xml version="1.0" encoding="utf-16"?><feed xmlns="http://www.w3.org/2005/Atom">

No matter what I try I can’t get it to return utf-8 instead of utf-16. When it is utf-16 it fails to work with IE8 but utf-8 works OK. How do I know it works OK I hear you ask if I can’t get it to return utf-8, well I end up using this line of code to return the feed to the browser:

Response.Write(Replace(xmlResult.ToString, "utf-16", "utf-8"))

Which is surely the worst hack you’ve seen today, if it isn’t I feel sorry for you!

So this is what I have tried to get UTF-8:

Response.ContentEncoding = System.Text.Encoding.UTF8
Response.Charset = "UTF-8"
settings.Encoding = System.Text.Encoding.UTF8

The settings line is what I thought would work but isn’t for me. My understanding is that the XML Writer is the part that is adding the XML declaration but as the settings.Encoding doesn’t work I’m stumped. Please help!

Thanks

  • 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-12T19:38:40+00:00Added an answer on May 12, 2026 at 7:38 pm

    The underlying reason is that when you call XmlWriter.Create with a StringBuilder, it creates a new StringWriter instance, which always has an encoding of UTF-16, and creates an XmlWriter around that StringWriter. Looking in Reflector, it looks like the encoding of StringWriter is a private, static variable, so all StringWriter instances will have the same encoding.

    Is there some reason you’re not creating an XmlWriter that directly writes to the Response OutputStream?

    If you can do that, this code works for me:

    With Response
      .ContentEncoding = Encoding.UTF8
      .ContentType = "text/xml"
    End With
    
    Dim atomFormatter As Atom10FeedFormatter = New Atom10FeedFormatter(feed)
    Dim settings As XmlWriterSettings = New XmlWriterSettings
    
    With settings
      .OmitXmlDeclaration = False
      .Encoding = Encoding.UTF8
    End With
    
    Using atomWriter As XmlWriter = XmlWriter.Create(Response.OutputStream, settings)
      atomFormatter.WriteTo(atomWriter)
      atomWriter.Close()
    End Using
    
    Response.End()
    

    If you must use StringBuilder for a good reason, perhaps you might consider omitting the Xml Declaration and adding it in your code before the atomFormatter.WriteTo call. Alternatively, you could write your function to accept an arbitrary Stream and create an XmlWriter on that, using a MemoryStream or FileStream when you aren’t planning to write directly to the Response.OutputStream.

    Apologies if the code is not idiomatic VB; I mostly write in C#.

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

Sidebar

Ask A Question

Stats

  • Questions 213k
  • Answers 213k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think you have an error in one case: For… May 12, 2026 at 10:36 pm
  • Editorial Team
    Editorial Team added an answer I believe that is the peek-a-boo bug in IE6. just… May 12, 2026 at 10:36 pm
  • Editorial Team
    Editorial Team added an answer Overload resolution in C++ happens at compile-time, not run-time. The… May 12, 2026 at 10:36 pm

Related Questions

I am writing a CakePHP 1.2 app. I have a list of people that
I am trying to create an automated clicker program in C# to make some
I have a c# simple application that have to write some values in a
I'm kind of new to this so please bear with me... Let's say I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.