XML is UTF-16 encoding but I want UTF-8 encoding ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shailendra Batham

    XML is UTF-16 encoding but I want UTF-8 encoding ?

    Hi there XML Gurus ;)

    I am trying to use XML Serialization to create a xml from a class, this is the output which I get when I create the XML and put in a string variale.

    "<?xml version=\"1.0\" encoding=\"utf-16\"?><OutBS><F irstName>XML</FirstName><Last Name>Guru</LastName><Membe rName>guru@xml. com</MemberName></OutBS>"

    My question is why am I getting the encoding as UTF-16, I want a UTF-8 encoding, how can this be achieve by getting the output in the xout string variable.

    Help is really appreciated.
    thanks,
    shailendra batham

    // Create the xout xml here.
    OutBS obs = new OutBS();
    obs.FirstName = "XML";
    obs.LastName = "Guru";
    obs.MemberName = "guru@xml.c om";

    StringWriter sw = new StringWriter();
    XmlSerializer xs = new XmlSerializer(o bs.GetType());
    xs.Serialize(sw , obs);
    String xout = sw.ToString();

    public class OutBS
    {
    public String FirstName;
    public String LastName;
    public String MemberName;
    }

    --------------------------------------------------------------------------------

  • Chris Lovett

    #2
    Re: XML is UTF-16 encoding but I want UTF-8 encoding ?

    StringWriter can only produce UTF-16 by definition.
    If you want some other encoding use StreamWriter.
    "Shailendra Batham" <sgbatham@sbcgl obal.net> wrote in message news:vmPZc.1094 9$QJ3.1476@news svr21.news.prod igy.com...
    Hi there XML Gurus ;)

    I am trying to use XML Serialization to create a xml from a class, this is the output which I get when I create the XML and put in a string variale.

    "<?xml version=\"1.0\" encoding=\"utf-16\"?><OutBS><F irstName>XML</FirstName><Last Name>Guru</LastName><Membe rName>guru@xml. com</MemberName></OutBS>"

    My question is why am I getting the encoding as UTF-16, I want a UTF-8 encoding, how can this be achieve by getting the output in the xout string variable.

    Help is really appreciated.
    thanks,
    shailendra batham

    // Create the xout xml here.
    OutBS obs = new OutBS();
    obs.FirstName = "XML";
    obs.LastName = "Guru";
    obs.MemberName = "guru@xml.c om";

    StringWriter sw = new StringWriter();
    XmlSerializer xs = new XmlSerializer(o bs.GetType());
    xs.Serialize(sw , obs);
    String xout = sw.ToString();

    public class OutBS
    {
    public String FirstName;
    public String LastName;
    public String MemberName;
    }

    ------------------------------------------------------------------------------

    Comment

    • Derek Harmon

      #3
      Re: XML is UTF-16 encoding but I want UTF-8 encoding ?

      "Shailendra Batham" <sgbatham@sbcgl obal.net> wrote in message news:vmPZc.1094 9$QJ3.1476@news svr21.news.prod igy.com...[color=blue]
      > My question is why am I getting the encoding as UTF-16, I want a
      > UTF-8 encoding, how can this be achieve by getting the output in
      > the xout string variable.[/color]

      Shailendra,

      In .NET, a String is a sequence of zero or more Chars, and each
      Char is a two-byte Unicode character. Therefore, a String will
      always be UTF-16, and can be no other encoding.

      If you really care about getting UTF-8 encoding, then instead of
      serializing to a StringWriter you should serialize to a Memory-
      Stream wrapping a byte[]. You'd have decode the byte[] into
      UTF-16 to read it naturally, but the byte[] would contain UTF-8
      encoded data.

      - - - Utf8Serialize.c s (excerpt)
      using System.IO;
      using System.Text;
      using System.Xml;
      using System.Xml.Seri alization;
      // . . .
      MemoryStream memStrm = new MemoryStream( );
      XmlTextWriter xmlSink = new XmlTextWriter( memStrm, Encoding.UTF8);
      xs.Serialize( xmlSink, ob);

      byte[] utf8EncodedData = memStrm.GetBuff er( );
      // . . .
      - - -


      Derek Harmon


      Comment

      Working...