XMLTextWriter Encoding problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam Rothberg

    #1

    XMLTextWriter Encoding problem

    The following code sample should produce a valid xml file to the
    console. However, when I try this in C# (Visual Studio 2003, 1.1
    Framework), there is an extra questionmark preceding the rest of the
    content.

    MemoryStream ms = new MemoryStream();
    XmlTextWriter xtw = new XmlTextWriter(m s, Encoding.UTF8);
    xtw.Namespaces = false;
    xtw.Indentation = 5;
    xtw.Formatting = Formatting.Inde nted;
    xtw.WriteStartD ocument();
    xtw.WriteStartE lement("root");
    xtw.WriteStartE lement("People" );
    xtw.WriteStartE lement("Person" );
    xtw.WriteAttrib uteString("Firs tName", "John");
    xtw.WriteAttrib uteString("Last Name", "Smith");
    xtw.WriteEndEle ment();
    xtw.WriteStartE lement("Person" );
    xtw.WriteAttrib uteString("Firs tName", "Jane");
    xtw.WriteAttrib uteString("Last Name", "Smith");
    xtw.WriteEndEle ment();
    xtw.WriteEndEle ment();
    xtw.WriteEndEle ment();
    xtw.WriteEndDoc ument();
    xtw.Flush();
    xtw.Close();

    Console.WriteLi ne( Encoding.UTF8.G etString( ms.ToArray()));

    Everything works as expected when I use ASCII encoding. Any encoding
    works when I write to a file instead of a memory stream.

    Anyone have the answer to this problem?

    Thanks in advance.
  • Kevin Yu [MSFT]

    #2
    RE: XMLTextWriter Encoding problem

    Hi Adam,

    The first three bytes in the byte array are BOM(Byte Order Mark 0xEFBBBF),
    allowing applications to easily detect UTF-8 encoded text. If you want to
    create UTF-8 encoded content without BOM, don't use the default instance
    available through Encoding.UTF8, but create a new instance:

    Encoding utf8 = new UTF8Encoding(fa lse);

    In you program, you can do like the following:

    XmlTextWriter xtw = new XmlTextWriter(m s, new UTF8Encoding(fa lse));

    If anything is unclear, please feel free to reply to the post.

    Kevin Yu
    =======
    "This posting is provided "AS IS" with no warranties, and confers no
    rights."

    Comment

    • Adam Rothberg

      #3
      Re: XMLTextWriter Encoding problem

      Thank you. That worked great.


      v-kevy@online.mic rosoft.com (Kevin Yu [MSFT]) wrote in message news:<uHl4PnTxD HA.3660@cpmsftn gxa07.phx.gbl>. ..[color=blue]
      > Hi Adam,
      >
      > The first three bytes in the byte array are BOM(Byte Order Mark 0xEFBBBF),
      > allowing applications to easily detect UTF-8 encoded text. If you want to
      > create UTF-8 encoded content without BOM, don't use the default instance
      > available through Encoding.UTF8, but create a new instance:
      >
      > Encoding utf8 = new UTF8Encoding(fa lse);
      >
      > In you program, you can do like the following:
      >
      > XmlTextWriter xtw = new XmlTextWriter(m s, new UTF8Encoding(fa lse));
      >
      > If anything is unclear, please feel free to reply to the post.
      >
      > Kevin Yu
      > =======
      > "This posting is provided "AS IS" with no warranties, and confers no
      > rights."[/color]

      Comment

      • Christoph Schittko [MVP]

        #4
        Re: XMLTextWriter Encoding problem

        Kevin,

        could you explain what the problem is with using the default instance?

        Thanks,
        Christoph Schittko [MVP, XmlInsider]
        Software Architect, .NET Mentor

        "Kevin Yu [MSFT]" <v-kevy@online.mic rosoft.com> wrote in message
        news:uHl4PnTxDH A.3660@cpmsftng xa07.phx.gbl...[color=blue]
        > Hi Adam,
        >
        > The first three bytes in the byte array are BOM(Byte Order Mark 0xEFBBBF),
        > allowing applications to easily detect UTF-8 encoded text. If you want to
        > create UTF-8 encoded content without BOM, don't use the default instance
        > available through Encoding.UTF8, but create a new instance:
        >
        > Encoding utf8 = new UTF8Encoding(fa lse);
        >
        > In you program, you can do like the following:
        >
        > XmlTextWriter xtw = new XmlTextWriter(m s, new UTF8Encoding(fa lse));
        >
        > If anything is unclear, please feel free to reply to the post.
        >
        > Kevin Yu
        > =======
        > "This posting is provided "AS IS" with no warranties, and confers no
        > rights."
        >[/color]


        Comment

        • Kevin Yu [MSFT]

          #5
          Re: XMLTextWriter Encoding problem

          Hi Christoph,

          The UTF8 is a static instance of UTF8Encoding class. The encoder using this
          instance will emit an UTF8 identifier, which is the 3 bytes I mentioned in
          my last post. The identifer will help the program to recognize the UTF8
          encoding. If we create a new instance with the first parameter of the
          constructor set to false, it will not be emited.

          Kevin Yu
          =======
          "This posting is provided "AS IS" with no warranties, and confers no
          rights."

          Comment

          • Christoph Schittko [MVP]

            #6
            Re: XMLTextWriter Encoding problem

            Thanks :)

            --
            HTH
            Christoph Schittko [MVP]
            Software Architect, .NET Mentor
            "Kevin Yu [MSFT]" <v-kevy@online.mic rosoft.com> wrote in message
            news:V0VruNhxDH A.2604@cpmsftng xa07.phx.gbl...[color=blue]
            > Hi Christoph,
            >
            > The UTF8 is a static instance of UTF8Encoding class. The encoder using[/color]
            this[color=blue]
            > instance will emit an UTF8 identifier, which is the 3 bytes I mentioned in
            > my last post. The identifer will help the program to recognize the UTF8
            > encoding. If we create a new instance with the first parameter of the
            > constructor set to false, it will not be emited.
            >
            > Kevin Yu
            > =======
            > "This posting is provided "AS IS" with no warranties, and confers no
            > rights."
            >[/color]


            Comment

            Working...