0

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

2 Answers 2

3

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";
Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

1) i just do not understand what line i have to remove? 2)do i need to change encoding='UTF-8' from encoding='UTF-16' ?
You need to change the encoding from UTF-8 to UTF-16
The above code is loading the xml into a Document object and then setting its UTF encoding to UTF-16. If you have the Xml as a string then changing it to UTF-16 directly and then passing it to the DB should solve it. Else try using tools like Xml Spy and change the encoding of the xml validate if its a valid xml and once its valid try to persist it to the database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.