将XML字符串反序列化为对象类型时出错

时间:2017-11-01 11:03:01

标签: c# asp.net-mvc web-services asp.net-web-api c#-4.0

我在SOAP XML格式的应用程序中使用第三方Web服务。我们得到的回应也是XML格式。在将XML字符串反序列化为对象类型时,我们得到了" XML文档中存在错误(1,5584)。在反序列化之前,我已经检查了XML,它在我提到的位置看起来很完美。谁能说出这里的问题是什么?

            HttpWebRequest request = CreateWebRequest("GetEmploymentVerificationFields");
            XmlDocument soapEnvelopeXml = new XmlDocument();

            var loadXML = @"<?xml version=""1.0"" encoding=""utf-8""?><soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:v29=""https://e-verify.uscis.gov/WebService/EmployerService/V29""><soapenv:Header><wsse:Security soapenv:mustUnderstand=""1"" xmlns:wsse=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"" xmlns:wsu=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd""><wsse:UsernameToken wsu:Id=""UsernameToken-BEC9D84D8B68A3118D14543420311491""><wsse:Username></wsse:Username><wsse:Password></wsse:Password><wsse:Nonce EncodingType=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"">hBcjVXk/NxiSiva5xXKphA==</wsse:Nonce><wsu:Created>2017-10-10T12:40:11.947Z</wsu:Created></wsse:UsernameToken></wsse:Security></soapenv:Header><soapenv:Body><GetEmploymentVerificationFields xmlns=""https://e-verify.uscis.gov/WebService/EmployerService/V29""><request xmlns:b=""https://e-verify.uscis.gov/WebService/EmployerService/V29/"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><b:CitizenshipStatusCode i:nil=""true"">";
            loadXML += model.CitizenshipStatusCode + @"</b:CitizenshipStatusCode><b:DocumentTypeId i:nil=""true"">";
            loadXML += model.DocumentTypeId + @"</b:DocumentTypeId><b:ListBDocumentTypeId i:nil=""true"">";
            loadXML += model.ListBDocumentTypeId + @"</b:ListBDocumentTypeId><b:SupportingDocumentTypeId i:nil=""true"">";
            loadXML += model.SupportingDocumentTypeId + @"</b:SupportingDocumentTypeId><b:IssuingAuthorityCode i:nil=""true"">";
            loadXML += model.IssuingAuthorityCode + "</b:IssuingAuthorityCode></request></GetEmploymentVerificationFields></soapenv:Body></soapenv:Envelope>";

            soapEnvelopeXml.LoadXml(loadXML);

            using (Stream stream = request.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }

            using (WebResponse response = request.GetResponse())
            {
                using (StreamReader rd = new StreamReader(response.GetResponseStream()))
                {
                    string soapResult = rd.ReadToEnd();
                    soapResult = soapResult.Replace("xmlns=\"https://e-verify.uscis.gov/WebService/EmployerService/V29\"", "");
                    soapResult = soapResult.Replace("xmlns:a=\"https://e-verify.uscis.gov/WebService/EmployerService/V29/\"", "");
                    soapResult = soapResult.Replace("xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"", "");
                    soapResult = soapResult.Replace("a:", "");
                    soapResult = soapResult.Replace("i:nil=\"true\"", "");

                    var firstIndex = soapResult.IndexOf("GetEmploymentVerificationFieldsResult");
                    var lastIndex = soapResult.LastIndexOf("GetEmploymentVerificationFieldsResult");
                    var count = "GetEmploymentVerificationFieldsResult".Length + 2;
                    var data = soapResult.Substring(firstIndex - 1, (lastIndex - firstIndex) + count);

                    XmlSerializer serializer = new XmlSerializer(typeof(GetEmploymentVerificationFieldsResult));
                    using (TextReader reader = new StringReader(data))
                    {
                        GetEmploymentVerificationFieldsResult result = (GetEmploymentVerificationFieldsResult)serializer.Deserialize(reader);
                        return result;
                    }
                }
            }

0 个答案:

没有答案