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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:05:27+00:00 2026-05-23T02:05:27+00:00

For some reason the following code produces XML that does not contain a declaration:

  • 0

For some reason the following code produces XML that does not contain a declaration:

        XDocument xDocument = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
            new XElement("project",
                new XAttribute("number", project.ProjectNumber),
                new XElement("store",
                    new XAttribute("number", project.StoreNumber)
                ),

                // User Element
                new XElement("user", 
                    new XAttribute("owner-id", project.OwnerID ?? 0),
                    new XElement("email", new XCData(project.OwnerEmail ?? "")),
                    new XElement("project-name", new XCData(project.ProjectName ?? ""))
                ),

                // Nested Project Element
                new XElement("nested-project", 
                    new XAttribute("version", new Version(1, 0)),
                    new XElement("project", 
                        new XAttribute("version", new Version(1, 0)),
                        xProjectItems = new XElement("project-items")
                    ),
                    new XElement("price-per-part", project.PricePerPart),
                    new XElement("sheet-quantity", project.SheetQuantity),
                    new XElement("edge-length", project.EdgeLength),
                    new XElement("price", project.Price),
                    new XElement("status", project.Status),
                    xMaterialItems = new XElement("alternative-material-items"),
                    xProductItems = new XElement("project-product-items")
                )
            )
        );

        String strXML = xDocument.ToString();

It has produced a declaration before. Am I missing something obvious?

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-23T02:05:28+00:00Added an answer on May 23, 2026 at 2:05 am

    The XDeclaration will be available when you use one of the XDocument.Save methods. For example:

    var doc = new XDocument (
                new XDeclaration ("1.0", "utf-8", "yes"),
                new XElement ("test", "data")
            );
    
    string path = Path.Combine(Path.GetTempPath(), "temp.xml");
    doc.Save(path);
    Console.WriteLine(File.ReadAllText(path));
    

    Alternately you could use this approach:

    var sw = new StringWriter();
    doc.Save(sw);
    string result = sw.GetStringBuilder().ToString();
    Console.WriteLine(result);
    

    EDIT: note that some approaches will convert the utf-8 designation to utf-16. If you want to force it to be utf-8 you would have to use this different approach:

    using (var mem = new MemoryStream())  
    using (var writer = new XmlTextWriter(mem, System.Text.Encoding.UTF8))
    {
        writer.Formatting = Formatting.Indented;
        doc.WriteTo(writer);
        writer.Flush();
        mem.Flush();
        mem.Seek(0, SeekOrigin.Begin);
        using (var reader = new StreamReader(mem))
        {
            var xml = reader.ReadToEnd();
            Console.WriteLine(xml);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason the following code does not work as expected when in IE
I'm not sure why, but for some reason, the following code skips the else
For some reason the following code fails. You can't simply erase a reverse_iterator by
For some reason, the following code never calls Event::Event(Event&& e) Event a; Event b;
I have the following code: visitSite.hidden = YES; For some reason, when I click
I have the following code: <%= select_tag :role, options_for_select(Project::COMPANY_ROLES.concat(['Other...']), @relationship.role) %> For some reason
The following html code works in Firefox, but for some reason fails in IE
For some reason the following doesn't crash like my program does, but I'm pretty
For some reason, I am not entirely sure why, but the following is not
I'm test building a scraping site with django. For some reason the following code

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.