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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:21:01+00:00 2026-05-11T16:21:01+00:00

This is C#/.NET 2.0. So I have string that contains the future contents of

  • 0

This is C#/.NET 2.0.

So I have string that contains the future contents of an XML file. It contains metadata and binary data from image files. I would like to somehow determine how big the XML file will be once I write the data in the string to the file system.

I’ve tried the following and neither works:

Console.Out.WriteLine("Size: " + data.Length/1024 + "KB");

and

Console.Out.WriteLine("Size: " + (data.Length * sizeof(char))/1024 + "KB");

Neither works (the actual size of the resulting file deviates from what is returned from either of these methods). I’m obviously missing something here. Any help would be appreciated.

XML Serialization:

// doc is an XMLDocument that I've built previously
StringWriter sw = new StringWriter();
doc.Save(sw);
string XMLAsString = sw.ToString();

Writing to file system (XMLAsString passed to this function as variable named data):

Random rnd = new Random(DateTime.Now.Millisecond);      
FileStream fs = File.Open(@"C:\testout" + rnd.Next(1000).ToString() +  ".txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
app.Diagnostics.Write("Size of XML: " + (data.Length * sizeof(char))/1024 + "KB");
sw.Write(data);
sw.Close();
fs.Close();

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-11T16:21:01+00:00Added an answer on May 11, 2026 at 4:21 pm

    You’re missing how the encoding process works. Try this:

    string data = "this is what I'm writing";
    byte[] mybytes = System.Text.Encoding.UTF8.GetBytes(data);
    

    The size of the array is exactly the number of bytes that it should take up on disk if it’s being written in a somewhat “normal” way, as UTF8 is the default encoding for text output (I think). There may be an extra EOF (End Of File) character written, or not, but you should be really close with that.

    Edit: I think it’s worth it for everybody to remember that characters in C#/.NET are NOT one byte long, but two, and are unicode characters, that are then encoded to whatever the output format needs. That’s why any approach with data.Length*sizeof(char) would not work.

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

Sidebar

Related Questions

I have a string variable that contains the following html data: <p> <em><strong>This is
I have a string that contains well formed xml in it. I want to
(In .NET) I have arbitrary binary data stored in in a byte[] (an image,
I have a string that is returned, that contains data separated by |'s. I
I'm using asp.net 4 and c#. I have a string that can contains: Special
I have this string in vb.net. I would appreciate if you can let me
Using VB.net (.net 2.0) I have a string in this format: record1_field1,record1_field2,record2_field3,record2_field1,record2_field2, etc... I
In code-behind of an ASP.NET page I have this method: public string TestFunc() {
I have this .NET web app that draws a table in Page_Load. Then after
So I have this ASP.Net 2.0 website that uses functions contained within a JS

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.