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 8071749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:47:31+00:00 2026-06-05T13:47:31+00:00

Im creating a class and converting it into xml. The problem is that when

  • 0

Im creating a class and converting it into xml.

The problem is that when i convert the class xml string into bytes
the ASCII.GetBytes return a byte array with
an extra character in the beginning of the ascArray

It’s always a ? character so the xml starts like this

?<?xml version="1.0" encoding="utf-8"?>

Why is this happening?

This is the code:

  WorkItem p = new WorkItem();

  // Fill the class with whatever need to be sent to client
  OneItem posts1 = new OneItem();
  posts1.id = "id 1";
  posts1.username = "hasse";
  posts1.message = "hej again";
  posts1.time = "time1";
  p.setPost(posts1);

  OneItem posts2 = new OneItem();
  posts2.id = "id 2";
  posts2.username = "bella";
  posts2.message = "hej again again";
  posts2.time = "time2";
  p.setPost(posts2);

  // convert the class WorkItem to xml
  MemoryStream memoryStream = new MemoryStream();
  XmlSerializer xs = new XmlSerializer(typeof(WorkItem));
  XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
  xs.Serialize(xmlTextWriter, p);

  // send the xml version of WorkItem to client
  byte[] data = memoryStream.ToArray();
  clientStream.Write(data, 0, data.Length);
  Console.WriteLine(" send.." + data);
  clientStream.Close();
  • 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-06-05T13:47:34+00:00Added an answer on June 5, 2026 at 1:47 pm

    I strongly suspect that the data starts with a byte order mark, which can’t be represented in ASCII.

    It’s not clear why you’re doing what you’re doing in the first place, particularly around the MemoryStream. Why are you creating a UTF-8 encoded byte array, then decoding that to a string (and we don’t know what UTF8ByteArrayToString does), then converting it back to a byte array? Why not just write the byte array straight to the client to start with? If you need the data as a string, I’d use a subclass of StringWriter which advertises that it uses UTF-8 as the encoding. If you don’t need it as a string, just stick to the byte array.

    Note that even aside from this first character, the fact that you’ve got an XML document encoded in UTF-8 means there may well be other non-ASCII characters in the string. Why are you using ASCII at all here?

    EDIT: Just to be clear, you’re fundamentally applying a lossy transformation, and doing it needlessly. Even if you want a local copy of the data, you should have something like this:

    // Removed bad try/catch block - don't just catch Exception, and don't
    // just swallow exceptions
    MemoryStream memoryStream = new MemoryStream();
    XmlSerializer xs = new XmlSerializer(typeof(WorkItem));
    XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
    xs.Serialize(xmlTextWriter, p);
    
    // Removed pointless conversion to/from string
    // Removed pointless BinaryWriter (just use the stream)
    
    // An alternative would be memoryStream.WriteTo(clientStream);
    byte[] data = memoryStream.ToArray();
    clientStream.Write(data, 0, data.Length);
    Console.WriteLine(" send.." + data);
    
    // Removed Close calls - you should use "using" statements to dispose of
    // streams automatically.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I m creating a class that extends AlertDialog. and setting layout. But when pop
I am creating a class that is mapped to an existing global so it
I'm creating a class that creates arrays. (Yes, I realize that's already available in
I'm creating a class (say, C) that associates data (say, D) with an object
I'm creating a class that derives from List... public class MyList : List<MyListItem> {}
I am creating a class that implements java.util.List . My class is using E
Im creating a UITableView inside my UINavigationController, and the problem is that the table
Creating a class at runtime is done as follows: klass = Class.new superclass, &block
I'm creating a class. What's the best practice for naming properties/methods when your preferred
I am creating a class for an application backbone and I need this function

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.