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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:20:24+00:00 2026-05-12T06:20:24+00:00

I am building an XmlDocument on the fly in .NET with an xml document.

  • 0

I am building an XmlDocument on the fly in .NET with an xml document. I then transform that with the Transform() method of an XslCompiledTransform.

The Transform() method threw an exception because an invalid character for the encoding was found in the stream. When I copy/paste the string with the help of the TextVisualizer in Visual Studio into Altova XmlSpy, it does not find an encoding problem.

I tried adding a UTF-16 header to the document to make it render as UTF-16 and calling Transform from the resulting text led to it complain about a BOM. Below is a simplified version of the code I used.

            XmlDocument document = new XmlDocument();
            XmlDeclaration decl = document.CreateXmlDeclaration("1.0", "UTF-16", null);
            document.AppendChild(decl);

            XmlNode root = document.CreateNode(XmlNodeType.Element, "RootNode", "");
            XmlNode nodeOne = document.CreateNode(XmlNodeType.Element, "FirstChild", null);
            XmlNode nodeTwp = doc.CreateNode(XmlNodeType.Element, "Second Child", null);

            root.AppendChild(nodeOne);
            root.AppendChild(nodeTwo);
            document.AppendChild(root);

Which I consequently am writing to a string like so:

        StringBuilder sbXml = new StringBuilder();
        using (XmlWriter wtr = XmlWriter.Create(sbXml))
        {
            xml.WriteTo(wtr);
            // More code that calls sbXml.ToString());
        }

What must I do to add the BOM or get XslCompiledTransform.Transform to not care about the bom?

  • 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-12T06:20:24+00:00Added an answer on May 12, 2026 at 6:20 am

    You don’t need to manually add the xml declaration.

    This code will add the BOM and the declaration to the output.

    XmlDocument document = new XmlDocument(); 
    // XmlDeclaration decl = document.CreateXmlDeclaration("1.0", "UTF-16", null); 
    // document.AppendChild(decl); 
    XmlNode root = document.CreateNode(XmlNodeType.Element, "RootNode", ""); 
    XmlNode nodeOne = document.CreateNode(XmlNodeType.Element, "FirstChild", null);
    XmlNode nodeTwo = document.CreateNode(XmlNodeType.Element, "SecondChild", null); 
    root.AppendChild(nodeOne); 
    root.AppendChild(nodeTwo); 
    document.AppendChild(root);
    
    using(MemoryStream ms = new MemoryStream())
    {
        StreamWriter sw = new StreamWriter(ms, Encoding.Unicode);
        document.Save(sw);
        Console.Write(System.Text.Encoding.Unicode.GetString(ms.ToArray()));
    }
    

    If you need the output as a byte[], you can use the output from ms.ToArray(). Otherwise you can use the appropriate System.Text.Encoding encoding to translate the byte[] into a variety of encodings.

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

Sidebar

Ask A Question

Stats

  • Questions 189k
  • Answers 189k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes , it is possible to have an animation when… May 12, 2026 at 5:47 pm
  • Editorial Team
    Editorial Team added an answer Have you tried the following: <ItemTemplate> <%# ShowDescription((ClassName)Container.DataItem) %> </ItemTemplate> May 12, 2026 at 5:47 pm
  • Editorial Team
    Editorial Team added an answer While this may not be the cleanest way , you… May 12, 2026 at 5:47 pm

Related Questions

I am building an XML document successfully with the following code: public function build($result)
I am about to build a piece of a project that will need to
I am new to C# so I apologize if this is a simple task.
I'm trying to serialize some data to xml in a way that can be

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.