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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:46:22+00:00 2026-06-03T08:46:22+00:00

I am trying to write an object to an Xml string and take that

  • 0

I am trying to write an object to an Xml string and take that string and save it to a DB. But first I need to get the string…

    private static readonly Encoding LocalEncoding = Encoding.UTF8;

    public static string SaveToString<T> (T settings)
    {
        Stream stream = null;
        TextWriter writer = null;
        string settingsString = null;

        try
        {
            stream = new MemoryStream();

            var serializer = new XmlSerializer(typeof(T));

            writer = new StreamWriter(stream, LocalEncoding);

            serializer.Serialize(writer, settings);

            var buffer = new byte[stream.Length];

            stream.Read(buffer, 0, (int)stream.Length);
            
            settingsString = LocalEncoding.GetString(buffer);
        }
        catch (Exception ex)
        {
            // If the action cancels we don't want to throw, just return null.
        }
        finally
        {
            if (stream != null)
                stream.Close();

            if (writer != null)
                writer.Close();
        }

        return settingsString;
    }

This seems to work, the stream gets filled with bytes. But when I come to read it back into the buffer and then into the string… the buffer is filled with ‘0’! Not sure what I doing wrong here guys.

  • 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-03T08:46:23+00:00Added an answer on June 3, 2026 at 8:46 am

    If you’d checked the results of stream.Read, you’d have seen that it hadn’t read anything – because you haven’t rewound the stream. (You could do this with stream.Position = 0;.) However, it’s easier to just call ToArray:

    settingsString = LocalEncoding.GetString(stream.ToArray());
    

    (You’ll need to change the type of stream from Stream to MemoryStream, but that’s okay as it’s in the same method where you create it.)

    Alternatively – and even more simply – just use StringWriter instead of StreamWriter. You’ll need to create a subclass if you want to use UTF-8 instead of UTF-16, but that’s pretty easy. See this answer for an example.

    I’m concerned by the way you’re just catching Exception and assuming that it means something harmless, by the way – without even logging anything. Note that using statements are generally cleaner than writing explicit finally blocks.

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

Sidebar

Related Questions

I am trying write a function that generates simulated data but if the simulated
I am trying a write a program to serialize a object to a xml
I'm trying to write a class that would enable me to just write .save();
I'm trying to write a simple config in C# that reads XML and customizes
Hey I'm trying to write a function that calls a static function based upon
I am trying to write a code that helps me to create a XML
I am trying to reproduce something that System.Xml.Serialization already does, but for a different
I'm trying to write a COM++ object wrapper around a Qt widget (control) I
Trying to write a function to see how often an object exists and give
I'm trying to write a linq to object query in vb.net, here is the

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.