Keith Costa
Keith Costa

Reputation: 1793

Getting error for inserting xml data into database table using C#

I am just trying to insert my xml data into a table through calling a stored procedure. I doubt something is wrong in my xml and that is why I am getting the error.

My xml is bit large & here I am showing just parts of it:

<?xml version='1.0' encoding='UTF-8' ?>
<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'   xmlns:ns='http://fedex.com/ws/ship/v9' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
    <soapenv:Body>
        <ns:ProcessShipmentRequest>
            <ns:WebAuthenticationDetail>
                <ns:UserCredential>
                    <ns:Key>aaaaaa</ns:Key>
                    <ns:Password>aaaaaa</ns:Password>
                </ns:UserCredential>
            </ns:WebAuthenticationDetail>
            <ns:ClientDetail>
                <ns:AccountNumber>aaaaaa</ns:AccountNumber>
                <ns:MeterNumber>aaaaaa</ns:MeterNumber>
            </ns:ClientDetail>
            <ns:TransactionDetail>
                <ns:CustomerTransactionId>ProcessShipmentRequest_DG_HAL</ns:CustomerTransactionId>
            </ns:TransactionDetail>
            <ns:Version>
                <ns:ServiceId>ship</ns:ServiceId>
                <ns:Major>9</ns:Major>
                <ns:Intermediate>0</ns:Intermediate>
                <ns:Minor>0</ns:Minor>
            </ns:Version>
            <ns:RequestedShipment>
                <ns:ShipTimestamp>2011-11-03T06:50:40</ns:ShipTimestamp>
                <ns:DropoffType>REGULAR_PICKUP</ns:DropoffType>
                <ns:ServiceType>FEDEX_GROUND</ns:ServiceType>
                <ns:PackagingType>YOUR_PACKAGING</ns:PackagingType>
                <ns:TotalWeight>
                     <ns:Units>LB</ns:Units>
                     <ns:Value>1.0</ns:Value>
                </ns:TotalWeight>
                <ns:Shipper>
                     <ns:AccountNumber>aaaaaa</ns:AccountNumber>
                     <ns:Contact>
                          <ns:CompanyName>test</ns:CompanyName>
                          <ns:PhoneNumber>11111111</ns:PhoneNumber>
                     </ns:Contact>
                     <ns:Address>

Here is my c# code for calling the stored procedure to insert data into table

private void SaveData(string CompanyName, string Attention, string Address1, string Address2, string Country, string PostalCode, string City, string State, string Telephone, string UPSService, bool isInsured, string IdentificationNo,  string RequestXml, string ResponseXml)
{
        Database DB = GlobalObjects.GetInstance().DB;

        using (DbCommand cmd = DB.GetStoredProcCommand("InsLabelDetail"))
        {
            DB.AddInParameter(cmd, "@IdentificationNo", DbType.String, IdentificationNo);
            DB.AddInParameter(cmd, "@CompanyName", DbType.String, CompanyName);
            DB.AddInParameter(cmd, "@Attention", DbType.String, Attention);
            DB.AddInParameter(cmd, "@add1", DbType.String, Address1);
            DB.AddInParameter(cmd, "@add2", DbType.String, Address2);
            DB.AddInParameter(cmd, "@Country", DbType.String, Country);
            DB.AddInParameter(cmd, "@PostalCode", DbType.String, PostalCode);
            DB.AddInParameter(cmd, "@City", DbType.String, City);
            DB.AddInParameter(cmd, "@State", DbType.String, State);
            DB.AddInParameter(cmd, "@Telephone", DbType.String, Telephone);
            DB.AddInParameter(cmd, "@UPSService", DbType.String, UPSService);
            DB.AddInParameter(cmd, "@isInsured", DbType.Int32, (isInsured==true ? 1 : 0));
            DB.AddInParameter(cmd, "@shipto", DbType.String, Country);
            DB.AddInParameter(cmd, "@RequestXML", DbType.Xml, RequestXml.Replace("encoding='UTF-8'", string.Empty));
            DB.AddInParameter(cmd, "@ResponseXML", DbType.Xml, ResponseXml.Replace("encoding='UTF-8'", string.Empty));

            try
            {
                DB.ExecuteNonQuery(cmd);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

The error or the exception I am getting is :

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

Please tell me where I made the mistake. Thanks

Upvotes: 0

Views: 1069

Answers (2)

Alper
Alper

Reputation: 789

I think you have a parsing error related to UTF-8, UTF-16 encodings. That means you must show us the data that parse that xml. With a lucky guess, I can say that you must change the code that you read the xml data with manual encoding switching. For Example:

XmlDocument _document = new XmlDocument();
_document.LoadXml(_xmlString);
XmlDeclaration _declaration = (XmlDeclaration)_document.FirstChild;
_declaration.Encoding = "UTF-16";

Upvotes: 3

Rajesh
Rajesh

Reputation: 7876

Can you try removing the line and see.

If it is successful then its comes down to the problem that xml in database is stored as UTF-16 and you are trying to insert using UTF-8.

Hope the information helps you.

Upvotes: 1

Related Questions