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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:29:00+00:00 2026-06-15T03:29:00+00:00

I`ve got a question about passing xml datatype to a query from C# code.

  • 0

I`ve got a question about passing xml datatype to a query from C# code.

First, here`s a table on SQL Server:

CREATE TABLE [dbo].[XmlTable](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [dat] [xml] NOT NULL
)

Second, I have an application which is written on C#. And I insert rows into the XmlTable from the application.

Here`s the code:

XDocument doc = new XDocument(new XElement("root",
     new XElement("abc", "123"),
     new XElement("cba", "321")));

var reader = new XmlTextReader(new StringReader(doc.ToString()));
var xml = new SqlXml(reader);

using (var connection = new SqlConnection(_connectionString))
{
     using (var command = new SqlCommand("insert into XmlTable (dat) values(@data)", connection))
     {
          command.Parameters.Add(new SqlParameter("@data", SqlDbType.Xml){Value = xml});
          connection.Open();
          command.ExecuteNonQuery();
     }
}

Everything works fine, rows are inserted. But if I run the SQL Profiler and inspect the insert command, I see following:

declare @p3 xml
set @p3=convert(xml,N'<root><abc>123</abc><cba>321</cba></root>')
exec sp_executesql N'insert into XmlTable (dat) values(@data)',N'@data xml',@data=@p3

We can see that in spite of passing SqlXml type into a query SqlClient performs unnecessary conversion from string to XML. XML parser does waste work.

The question is: why does SqlClient do this way? And how can I make it do without extra conversion?

  • 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-15T03:29:02+00:00Added an answer on June 15, 2026 at 3:29 am

    There is no converssion occuring in reallity. What you see is an artifact of Profiler/Trace having to somehow represent the TDS RPC call as a T-SQL batch you can copy paste and execute in SSMS. The real text executed is just insert into XmlTable (dat) values(@data) and @data type/value comes from the ParamData portion of the request. As with any wire protocol, serialization and encoding has to occur in the client and decoding and deserialization has to occur at the server. As a side note, there is no call to sp_execute_sql either, that is again an artifact of how Profiler/Trace displays an RPC call.

    this code is used in high-load scenario and additional convertation makes it slower

    That is speculation. You should base performance analysis on measurements and observations.

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

Sidebar

Related Questions

folks, i got a question about passing mutable object to a function with the
( This question about refactoring F# code got me one down vote, but also
I got question about log4net is it working on IIS 7,5? I read lot
I've got question about wpf xaml style definitions. When I try to set style
i got a question about unsigned char array. How can i store an integer
I got a question about good practice. I'm making a simple game and, I'm
i've got a question about the tables and stored procedures which are created by
i've got a question about how is it possible (if possible :) to use
I've got a question about SimpleDateFormat class and the java.util.Date's compareto method: I'm constructing
Hay I've got a question about relationships. I want to Users to have Friendships.

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.