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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:34:56+00:00 2026-05-17T19:34:56+00:00

I want to serialize my object to xml and then to a string. public

  • 0

I want to serialize my object to xml and then to a string.

  public class MyObject
  {
    [XmlElement]
    public string Name
    [XmlElement]
    public string Location;
  }

I want to obtain a single line string which will lok like this:

<MyObject><Name>Vladimir</Name><Location>Moskov</Location></MyObject>

I am using such code:

  XmlWriterSettings settings = new XmlWriterSettings();
  settings.OmitXmlDeclaration = true;
  settings.Indent = true;
  StringWriter StringWriter = new StringWriter();
  StringWriter.NewLine = ""; //tried to change it but without effect
  XmlWriter writer = XmlWriter.Create(StringWriter, settings);
  XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
  namespaces.Add(string.Empty, string.Empty);
  XmlSerializer MySerializer= new XmlSerializer(typeof(MyObject ));
  MyObject myObject = new MyObject { Name = "Vladimir", Location = "Moskov" };

  MySerializer.Serialize(writer, myObject, namespaces);
  string s = StringWriter.ToString();

This is the closest what I get:

<MyObject>\r\n  <Name>Vladimir</Name>\r\n  <Location>Moskov</Location>\r\n</MyObject>

I do know that I could remove “\r\n” from the string afterwards. But I would like to not produce them at all rather than removing them later.

Thanks for your time.

  • 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-17T19:34:56+00:00Added an answer on May 17, 2026 at 7:34 pm

    You could try:

    settings.NewLineHandling = NewLineHandling.None;
    settings.Indent = false;
    

    which for me, gives:

    <MyObject><Name>Vladimir</Name><Location>Moskov</Location></MyObject>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following 2 functions: public static string Serialize(object obj) { DataContractSerializer serializer
I am using Telerik controls in my webforms and want to serialize object on
I want to be able to load a serialized xml class to a Soap
I am trying to serialize my object to xml. A serializer seemingly serializes all
I'm trying to XML-serialize a class the properties of which I use to format
I want to set some attributes just before the object is serialized, but as
I'm currently convering my ASP.NET v2 application to serialize/deserialize it's objects because I want
I want to serialize my enum-value as an int, but i only get the
I have a collection of classes that I want to serialize out to an
in a C program I have an long* that I want to serialize (thus

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.