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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:14:23+00:00 2026-05-23T10:14:23+00:00

I have an XmlSerializer which I use to Serialize an object to an XDocument.

  • 0

I have an XmlSerializer which I use to Serialize an object to an XDocument.

var doc = new XDocument();
using (var writer = doc.CreateWriter())
{
   xmlSerializer.Serialize(writer, object);
}

After this is done, I want to add a XDeclaration:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

I construct this XDeclaration as described below:

var decl = new XDeclaration("1.0", "UTF-8", "no");

However, when I try to add this XDeclartion to my XDocument, I get the following error:

System.ArgumentException : Non white space characters cannot be added to content.

I searched Google for some time but all I’ve found is adding the XDeclaration to the constructor of the XDocument which in my case (when filling it with a XmlWriter) is not acceptable.

  • 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-23T10:14:24+00:00Added an answer on May 23, 2026 at 10:14 am

    Use property XDocument.Declaration


    EDIT:

    Sample code:

    var xmlSerializer = new XmlSerializer(typeof(int));
    
    var doc = new XDocument();
    
    var decl = new XDeclaration("1.0", "utf-8", "no");
    doc.Declaration = decl;
    
    using (var writer = doc.CreateWriter())
    {
        xmlSerializer.Serialize(writer, 1);
    }
    doc.Save(File.Create("x.xml"));
    

    This code produced following output:

    <?xml version="1.0" encoding="utf-8" standalone="no"?>
    <int>1</int>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I'm using XmlSerializer to serialize and deserialize an object. The xml is generated
I have an XmlSerializer object, and I have added 2 event handlers to the
I am using the XmlSerializer and have the following property in a class public
I'm getting an odd result when serializing a DateTime field using XmlSerializer. I have
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have a serialized object which needs to be sent as an encrypted XML
I have a BizTalk 2006 app which has a sendport using MSMQ. I have
In C#, how do I use an XmlSerializer to deserialize an object that might
I have a helper method that serialises an object, which works until you try
Suppose we have a class which can be serialized/deserialized by XmlSerializer. It would be

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.