1

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

XML parsing: line 1, character 39, unable to switch the encoding

This is the way I am generating my XML. The function is an extension function.

public static string ToXml<T>(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();
    }
}

and the stored procedure looks like this:

/*
        SET @Data = '<?xml version="1.0" encoding="utf-16"?>
                    <Students>
                      <Student>
                        <ID>2</ID>
                        <FirstName>Soument</FirstName>
                        <LastName>Sen</LastName>
                        <IsActive>true</IsActive>
                        <StateID>2</StateID>
                        <StateName>Bihar</StateName>
                        <CityID>4</CityID>
                        <CityName>DharBganga</CityName>
                      </Student>
                    </Students>'  

*/
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

Looking for guidance like how to fix it. Thanks

Loofer
  • 6,841
  • 9
  • 61
  • 102
Mou
  • 15,673
  • 43
  • 156
  • 275
  • where i can specify the encoding because i have not used `XmlTextWriter` – Mou Jan 08 '16 at 14:51
  • I'd suggest you simply omit the declaration (the duplicate has an answer that suggests this). Alternatively, you could do something like [this](http://stackoverflow.com/a/3862106/1320845). – Charles Mager Jan 08 '16 at 14:54
  • `omit the declaration` which declaration you are talking about? – Mou Jan 08 '16 at 14:58
  • The XML declaration. The answer I referred to is [this one](http://stackoverflow.com/a/14579572/1320845). – Charles Mager Jan 08 '16 at 14:59
  • sorry to request....can u post some code. i just do not understand what to omit in my code. thanks – Mou Jan 08 '16 at 15:04
  • The linked answer has the code. This really is a direct duplicate of the linked question. – Charles Mager Jan 08 '16 at 15:05

1 Answers1

0

The encoding="utf-16" in the XML header is killing you. If the XML header is necessary, can you get rid of the encoding?

The full legalese from MSDN is here; the point is that the encoding within the XML document has to match the encoding of the string containing the XML document.

S. Rojak
  • 454
  • 2
  • 6
  • i just like to know which line i need to change in my C# code to change utf-16 to utf-8 ? see my code and come with rectified version if possible. – Mou Jan 08 '16 at 17:48
  • See this posting: (http://stackoverflow.com/questions/1738511/easier-way-to-serialize-c-sharp-class-as-xml-text). – S. Rojak Jan 08 '16 at 17:54