title: Unable to switch the encoding
body: <p>I am generating an xml from my C# application which I am then sending to a SQL Server stored procedure. When the stored procedure parses the xml, then it is throwing error </p>
  
  <blockquote>
    <p>XML parsing: line 1, character 39, unable to switch the encoding</p>
  </blockquote>
  
  <p>This is the way I am generating my XML. The function is an extension function.</p>
  
  <pre><code>public static string ToXml&lt;T&gt;(this T obj, string rootName)
  {
      XmlSerializer serializer = new XmlSerializer(typeof(T), new XmlRootAttribute(rootName));
  
      var xmlNs = new XmlSerializerNamespaces();
      xmlNs.Add(string.Empty, string.Empty);
  
      using (StringWriter sw = new StringWriter())
      {
          serializer.Serialize(sw, obj, xmlNs);
          return sw.ToString();
      }
  }
  </code></pre>
  
  <p>and the stored procedure looks like this:</p>
  
  <pre><code>/*
          SET @Data = '&lt;?xml version="1.0" encoding="utf-16"?&gt;
                      &lt;Students&gt;
                        &lt;Student&gt;
                          &lt;ID&gt;2&lt;/ID&gt;
                          &lt;FirstName&gt;Soument&lt;/FirstName&gt;
                          &lt;LastName&gt;Sen&lt;/LastName&gt;
                          &lt;IsActive&gt;true&lt;/IsActive&gt;
                          &lt;StateID&gt;2&lt;/StateID&gt;
                          &lt;StateName&gt;Bihar&lt;/StateName&gt;
                          &lt;CityID&gt;4&lt;/CityID&gt;
                          &lt;CityName&gt;DharBganga&lt;/CityName&gt;
                        &lt;/Student&gt;
                      &lt;/Students&gt;'  
  
  */
  CREATE PROCEDURE [dbo].USP_SaveStudent
  (
          @Data               xml,
          @PageNbr            INT = 1,
          @PageSize           INT = 10,
          @SortColumn         NVARCHAR(20) = 'ID',
          @SortOrder          NVARCHAR(4)= 'ASC'  
  )
  AS
  BEGIN TRY
      MERGE INTO Student as Trg  
      USING (SELECT
                 d.x.value('id[1]', 'varchar(MAX)') as ID,
                 d.x.value('FirstName[1]', 'varchar(MAX)') as FirstName ,  
                 d.x.value('LastName[1]', 'varchar(MAX)') as LastName,
                 d.x.value('StateID[1]','int') as StateID,
                 d.x.value('CityID[1]','int') as CityID,
                 d.x.value('IsActive[1]','bit') as IsActive
             FROM
                 @Data.nodes('/Students/Student') as  d(x)) AS Src ON Trg.ID = Src.ID  
  
      WHEN MATCHED THEN
          UPDATE 
              SET Trg.FirstName = Src.FirstName,  
                  Trg.LastName = Src.LastName,
                  Trg.StateID = Src.StateID,
                  Trg.CityID = Src.CityID,
                  Trg.IsActive = Src.IsActive
  
      WHEN NOT MATCHED BY TARGET THEN
          INSERT (FirstName, LastName, StateID, CityID, IsActive) 
          VALUES(Src.FirstName, Src.LastName, Src.StateID, Src.CityID, Src.IsActive);   
  END TRY
  BEGIN CATCH
      -- Insert Error into table
      --INSERT INTO #error_log(message)
      --VALUES (ERROR_MESSAGE());
  END CATCH
  
  EXEC USP_GetStudentData @PageNbr, @PageSize, @SortColumn, @SortOrder
  </code></pre>
  
  <p>Looking for guidance like how to fix it. Thanks</p>