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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:21:00+00:00 2026-06-13T09:21:00+00:00

Consider the following code: [Test] public void StackOverflowQuestionTest() { const string connectionString = enter

  • 0

Consider the following code:

[Test]
    public void StackOverflowQuestionTest()
    {
        const string connectionString = "enter your connection string if you wanna test this code";

        byte[] result = null;
        using (var connection = new SqlConnection(connectionString))
        {
            connection.Open();
            using (var sqlCommand = new SqlCommand("declare @xml as xml = '<xml/>' SELECT convert(varbinary(max), @xml) as value"))
            //using (var sqlCommand = new SqlCommand("SELECT convert(varbinary(max), N'<xml/>') as value"))
            {
                sqlCommand.Connection = connection;

                using (SqlDataReader reader = sqlCommand.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        result = (byte[])reader["value"];
                    }

                    reader.Close();
                }
            }
        }

        string decodedString = new UnicodeEncoding(false, true).GetString(result);
        var document = XElement.Parse(decodedString);
    }

If I run this test I get an XmlException with message : “Data at the root level is invalid. Line 1, position 1.” As it turns out the problem is “0xFFFE” preamble which is considered as invalid character.
Note that if I use commented string instead, everything works just fine, which is strange as per me. Looks like SqlServer stores XML strings in UCS-2 with a BOM, and at the same time it stores nvarchar values without it.
The main question is: how can I decode this byte array to string which will not contain this preamble (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-06-13T09:21:01+00:00Added an answer on June 13, 2026 at 9:21 am

    In case anyone will need this in future, the following code works:

    using(var ms = new MemoryStream(result))
    {
        using (var sr = new StreamReader(ms, Encoding.Unicode, true))
        {
            decodedString = sr.ReadToEnd();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this code: class test { public static void main(String[] args) { test inst_test
Consider the following code, public class StartUp { public StartUp(String[] test){} public static void
Consider the following code: [Test] public void WidgetTest() { foreach (Widget widget in widgets)
Consider the following code: #include <iostream> struct test { void public_test() { [this]() {
Consider the following code Snippet Form form2 = new Form(); test(form2); form2.Show(); public void
Consider the following Java code: public class Test { private Foo< String, String >
consider the following test class: [TestClass] public class ExampleTests { [TestMethod] public void FileDoesNotExists()
Consider the following code snippet: class Test { public int Length{ get; set; }
I encountered this problem while solving a practice test Consider the following code written
Possible Duplicate: Static Initialization Blocks Consider the following code : public class Test {

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.