意外的XML声明。 XML声明必须是文档中的第一个节点

时间:2018-06-20 08:00:31

标签: c# xml soap

我正在尝试使用HttpUtility.Decode以正确的格式获取xml响应。在Xml Document中完成并将其加载后,它将引发以下异常: 意外的XML声明。 XML声明必须是文档中的第一个节点,并且不允许在其之前出现空格字符。第1行,位置313。 我不确定为什么,因为如果我没有记错的话,xml声明确实会作为第一个节点出现。我对XML还是很陌生,因此将不胜感激。我该如何解决?谢谢。

XML解码响应:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><Query_With_StringResponse xmlns="http://www.syspro.com/ns/query/"><Query_With_StringResult><?xml version="1.0" encoding="Windows-1252"?>
<ARListOfCustomers Language='05' Language2='EN' CssStyle='' DecFormat='1' DateFormat='01' Role='01' Version='7.0.005' OperatorPrimaryRole='   '   >
<QueryOptions>
<ReportSequence>CU</ReportSequence>
<PriceCode/>
<PriceProductmatrix>N</PriceProductmatrix>
<ExtraFields>N</ExtraFields>
<InterestExemptionStatusSelection>A</InterestExemptionStatusSelection>
<TaxExemptionSelection>A</TaxExemptionSelection>
<CustomerSelectionFilterType>A</CustomerSelectionFilterType>
<CustomerSelectionFilterValue/>
<CustomerClassSelectionFilterType>A</CustomerClassSelectionFilterType>
<CustomerClassSelectionFilterValue/>
<GeographicAreaSelectionFilterType>A</GeographicAreaSelectionFilterType>
<GeographicAreaSelectionFilterValue/>
<BranchSelectionFilterType>A</BranchSelectionFilterType>
<BranchSelectionFilterValue/>
<SalespersonSelectionFilterType>A</SalespersonSelectionFilterType>
<SalespersonSelectionFilterValue/>
<LineDiscountCodeSelectionFilterType>A</LineDiscountCodeSelectionFilterType>
<LineDiscountCodeSelectionFilterValue/>
<TermsSelectionFilterType>A</TermsSelectionFilterType>
<TermsSelectionFilterValue/>
<ProductCategorySelectionFilterType>A</ProductCategorySelectionFilterType>
<ProductCategorySelectionFilterValue/>
<InvoiceDiscountCodeSelectionFilterType>A</InvoiceDiscountCodeSelectionFilterType>
<InvoiceDiscountCodeSelectionFilterValue/>
<CurrencySelectionFilterType>A</CurrencySelectionFilterType>
<CurrencySelectionFilterValue/>
<CreditLimitSelectionFilterType>A</CreditLimitSelectionFilterType>
<CreditLimitSelectionFilterValue/>
</QueryOptions>
<Customer>
<CustomerListHeader>
<Customer>TSAR</Customer>
<CustomerName>TSAR BUSINESS SOLUTION</CustomerName>
<CustomerShortName>TSAR BUSINESS SOLUTI</CustomerShortName>
<Branch>TSAR</Branch>
<BranchDescription>HEAD OFFICE  TSAR</BranchDescription>
<Geography>031</Geography>
<GeographyDescription>DURBAN</GeographyDescription>
<Class/>
<ClassDescription>** Not on file **</ClassDescription>
<BalanceType>Op-item</BalanceType>
<Sales>IVAN</Sales>
<CreditLimit>           0</CreditLimit>
<Currency>R</Currency>
<CurrencyDescription>Rand</CurrencyDescription>
<Telephone/>
<InvoiceTermsCode>CO</InvoiceTermsCode>
<TermsCodeDescription>CASH ON DELIVERY</TermsCodeDescription>
</CustomerListHeader>
<CustomerListDetails>
<Contact/>
<TaxNo>Tax No:</TaxNo>
<SpecialInstructions/>
<SoldToAddress1/>
<SoldToAddress2/>
<SoldToAddress3/>
<SoldToAddress3Loc/>
<SoldToAddress4/>
<SoldToAddress5/>
<SoldToAddress6/>
<SoldToGpsLat>  0.000000</SoldToGpsLat>
<SoldToGpsLong>   0.000000</SoldToGpsLong>
<ShipToAddress1>STRAUSS DALY</ShipToAddress1>
<ShipToAddress2>41 RICHFONT CRICLE</ShipToAddress2>
<ShipToAddress3>DURBAN</ShipToAddress3>
<ShipToAddress3Loc/>
<ShipToAddress4>KZB</ShipToAddress4>
<ShipToAddress5>SOUTH AFRICA</ShipToAddress5>
<ShipToAddress6>4000</ShipToAddress6>
<ShipToGpsLat>  0.000000</ShipToGpsLat>
<ShipToGpsLong>   0.000000</ShipToGpsLong>
<GSTNumber/>
<LineDiscCode/>
<InvDiscCode/>
<DefaultPriceCode/>
<CompanyTaxNumber/>
<ExemptFinChg>No finance charges</ExemptFinChg>
</CustomerListDetails>
</Customer>
<ReportSummary>
<NoOfCustomersListed>    1</NoOfCustomersListed>
</ReportSummary>
</ARListOfCustomers>
 </Query_With_StringResult></Query_With_StringResponse></soap:Body></soap:Envelope>

我的代码:代码在“ doc.LoadXml(decodedXml);”上中断。

 public async Task<string> CreateSoapEnvelop()
        {
            string soapString = @"<?xml version=""1.0"" encoding=""utf-8""?>
            <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
                <soap:Body>
                 <Query_With_String xmlns=""http://www.syspro.com/ns/query/"">
                <UserId>" + Settings.GUID + @"</UserId>
                <BusinessObject></BusinessObject>
                <XMLIn></XMLIn>
                </Query_With_String>
                </soap:Body>
            </soap:Envelope>";
            try
            {
                HttpResponseMessage response = await PostXmlRequest("http://sysprowebservices/query.asmx", soapString);
                var soapResponse = await response.Content.ReadAsStringAsync();


                var decodedXml = HttpUtility.HtmlDecode(soapResponse);

                XmlDocument doc = new XmlDocument();
                doc.LoadXml(decodedXml);

                XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
                nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
                nsmgr.AddNamespace("ab", "http://www.syspro.com/ns/query/");
                nsmgr.AddNamespace("bg", " https://bixg.choicepoint.com/webservices/3.0");
                nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
                nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

                XmlNode xmlnode = doc.DocumentElement.SelectSingleNode("/soap:Envelope/soap:Body/ab:Query_With_StringResponse/ab:Query_With_StringResult", nsmgr);

                string customer = xmlnode.SelectSingleNode("CustomerName").InnerText;
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
            }
            return "";
        }

        public static async Task<HttpResponseMessage> PostXmlRequest(string baseUrl, string xmlString)
        {
            using (var httpClient = new HttpClient())
            {
                var httpContent = new StringContent(xmlString, Encoding.UTF8, "text/xml");
                httpContent.Headers.Add("SOAPAction", "http://www.syspro.com/ns/query/Query_With_String");

                return await httpClient.PostAsync(baseUrl, httpContent);

            }
        }

更新

{
                HttpResponseMessage response = await PostXmlRequest("http://196.37.159.30/sysprowebservices/query.asmx", soapString);
                var soapResponse = await response.Content.ReadAsStringAsync();


                XmlDocument doc = new XmlDocument();
                doc.LoadXml(soapResponse);


                XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
                nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
                nsmgr.AddNamespace("ab", "http://www.syspro.com/ns/query/");
                nsmgr.AddNamespace("bg", " https://bixg.choicepoint.com/webservices/3.0");
                nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
                nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

                XmlNode xmlnode = doc.DocumentElement.SelectSingleNode("/soap:Envelope/soap:Body/ab:Query_With_StringResponse/ab:Query_With_StringResult", nsmgr);
                var xmlDecoded = HttpUtility.HtmlDecode(xmlnode.ToString());


            }

从XmlDecode更新响应:“ System.Xml.XmlElement”

1 个答案:

答案 0 :(得分:2)

首先,除非您非常了解自己在做什么,否则不要滚动自己的SOAP客户端。您不能使用WCF吗?

但是您有一个返回XML的SOAP调用。您不能像对HTML那样对整个响应进行解码,因为这样内部XML的编码将丢失,从而导致XML文档无效。

您需要先阅读答复,然后获取内部XML字符串,然后对其进行解码:

XmlDocument doc = new XmlDocument();
doc.LoadXml(soapResponse);

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
nsmgr.AddNamespace("ab", "http://www.syspro.com/ns/query/");
nsmgr.AddNamespace("bg", " https://bixg.choicepoint.com/webservices/3.0");
nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

XmlNode xmlnode = doc.DocumentElement.SelectSingleNode("/soap:Envelope/soap:Body/ab:Query_With_StringResponse/ab:Query_With_StringResult", nsmgr);

现在xmlnode保存编码的XML。对其进行解码,然后随心所欲:

var decodedXml = HttpUtility.HtmlDecode(xmlnode.InnerXml);