簡體   English   中英

使用C#將xml數據插入數據庫表時出錯

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

我只是想通過調用存儲過程將xml數據插入表中。 我懷疑xml中出了點問題,這就是為什么我得到此錯誤。

我的xml有點大,在這里我只顯示其中的一部分:

<?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>

這是我的C#代碼,用於調用存儲過程以將數據插入表中

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;
            }
        }
    }

我得到的錯誤或異常是:

XML解析:第1行,字符38,無法切換編碼

請告訴我我在哪里弄錯了。 謝謝

我認為您有一個與UTF-8,UTF-16編碼有關的解析錯誤。 這意味着您必須向我們展示解析該xml的數據。 幸運的是,我可以說您必須通過手動編碼切換來更改讀取xml數據的代碼。 例如:

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

您可以嘗試刪除該行並查看。

如果成功,則歸結為以下問題:數據庫中的xml存儲為UTF-16,而您嘗試使用UTF-8進行插入。

希望這些信息對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM