How do I set the utf

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Hunt

    How do I set the utf

    The following is what I have :

    XmlWriterSettin gs xmlSettings = new XmlWriterSettin gs();
    xmlSettings.Ind ent = true;
    xmlSettings.Ind entChars = " ";
    xmlSettings.Enc oding = System.Text.Enc oding.UTF8;
    StringBuilder sb = new StringBuilder() ;
    using (XmlWriter writer = XmlWriter.Creat e(sb, xmlSettings))

    ============
    The problem I have is the xml comes out with
    <?xml Version="1.0" encoding="utf-16"?>

    It really does not matter what I have in the xmlSettings, it always comes
    out utf-16.

    ???? What am i missing ?????

    Thanks.






  • Anthony Jones

    #2
    Re: How do I set the utf

    "Phil Hunt" <aaa@aaa.comwro te in message
    news:uXiDRv51IH A.5048@TK2MSFTN GP06.phx.gbl...
    The following is what I have :
    >
    XmlWriterSettin gs xmlSettings = new XmlWriterSettin gs();
    xmlSettings.Ind ent = true;
    xmlSettings.Ind entChars = " ";
    xmlSettings.Enc oding = System.Text.Enc oding.UTF8;
    StringBuilder sb = new StringBuilder() ;
    using (XmlWriter writer = XmlWriter.Creat e(sb, xmlSettings))
    >
    ============
    The problem I have is the xml comes out with
    <?xml Version="1.0" encoding="utf-16"?>
    >
    It really does not matter what I have in the xmlSettings, it always comes
    out utf-16.
    >
    You are writing to a string builder. All strings in .NET are unicode
    (UTF-16) so it doesn't matter what encoding you set on settings because
    there isn't any encoding to be done.

    If you were writing to a File or some other stream it would work.

    --
    Anthony Jones - MVP ASP/ASP.NET


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: How do I set the utf

      On Jun 26, 3:49 pm, "Phil Hunt" <a...@aaa.comwr ote:
      The following is what I have :
      >
      XmlWriterSettin gs xmlSettings = new XmlWriterSettin gs();
      xmlSettings.Ind ent = true;
      xmlSettings.Ind entChars = " ";
      xmlSettings.Enc oding = System.Text.Enc oding.UTF8;
      StringBuilder sb = new StringBuilder() ;
      using (XmlWriter writer = XmlWriter.Creat e(sb, xmlSettings))
      >
      ============
      The problem I have is the xml comes out with
      <?xml Version="1.0" encoding="utf-16"?>
      >
      It really does not matter what I have in the xmlSettings, it always comes
      out utf-16.
      >
      ???? What am i missing ?????
      You need a StringWriterWit hEncoding, rather than passing in a
      StringBuilder directly.

      The StringWriterWit hEncoding code is really simple though:

      public class StringWriterWit hEncoding : StringWriter
      {
      Encoding encoding;
      public StringWriterWit hEncoding (Encoding encoding)
      {
      this.encoding = encoding;
      }

      public override Encoding Encoding
      {
      get { return encoding; }
      }
      }

      You could add other constructor overloads should you wish, of course.

      Jon

      Comment

      • Phil Hunt

        #4
        Re: How do I set the utf

        Thanks.



        "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
        news:f0e648fa-6ca6-4cb3-8017-68802d7413c2@x4 1g2000hsb.googl egroups.com...
        On Jun 26, 3:49 pm, "Phil Hunt" <a...@aaa.comwr ote:
        >The following is what I have :
        >>
        >XmlWriterSetti ngs xmlSettings = new XmlWriterSettin gs();
        >xmlSettings.In dent = true;
        >xmlSettings.In dentChars = " ";
        >xmlSettings.En coding = System.Text.Enc oding.UTF8;
        >StringBuilde r sb = new StringBuilder() ;
        >using (XmlWriter writer = XmlWriter.Creat e(sb, xmlSettings))
        >>
        >============
        >The problem I have is the xml comes out with
        ><?xml Version="1.0" encoding="utf-16"?>
        >>
        >It really does not matter what I have in the xmlSettings, it always comes
        >out utf-16.
        >>
        >???? What am i missing ?????
        >
        You need a StringWriterWit hEncoding, rather than passing in a
        StringBuilder directly.
        >
        The StringWriterWit hEncoding code is really simple though:
        >
        public class StringWriterWit hEncoding : StringWriter
        {
        Encoding encoding;
        public StringWriterWit hEncoding (Encoding encoding)
        {
        this.encoding = encoding;
        }
        >
        public override Encoding Encoding
        {
        get { return encoding; }
        }
        }
        >
        You could add other constructor overloads should you wish, of course.
        >
        Jon

        Comment

        Working...