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

C# 意外的XML声明。XML声明必须是文档中的第一个节点,c#,xml,soap,C#,Xml,Soap,我正在尝试使用HttpUtility.Decode以正确的格式获取xml响应。在执行此操作并将其加载到Xml文档中时,它会引发以下异常:意外的Xml声明。XML声明必须是文档中的第一个节点,并且前面不允许出现空白字符。第1行,位置313。我不知道为什么,因为如果我没有弄错的话,xml声明会作为第一个节点出现。我对XML相当陌生,因此任何帮助都将不胜感激。我该如何解决这个问题?谢谢 XML解码响应: <?xml version="1.0" encoding="utf-8"?><s

我正在尝试使用HttpUtility.Decode以正确的格式获取xml响应。在执行此操作并将其加载到Xml文档中时,它会引发以下异常:意外的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>

来自XmlDecode的更新响应:“System.Xml.xmlement”

首先,除非您非常清楚自己在做什么,否则不要使用自己的SOAP客户端。你不能改用WCF吗

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

您需要首先读取repsonse,然后获取内部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);

首先,除非您非常清楚自己在做什么,否则不要使用自己的SOAP客户端。你不能改用WCF吗

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

您需要首先读取repsonse,然后获取内部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);

我认为您不会以任何方式加载外部XML。@Janine“尝试了这个,但没有成功”-然后您做错了。编辑您的问题以显示您尝试了什么,如问题“XML解码响应”中的@bommelding所述。内部XML将被编码。我的答案中的代码是一个文本剪报,只添加了一些换行符。您认为您可以将其读入任何Xml api吗?@bommelding这是因为OP HTML解码了响应。实际响应将包含
?xml version=“1.0”encoding=“Windows-1252”
。我认为您不会以任何方式加载外部xml。@Janine“尝试了这个,但没有成功”-然后您做错了。编辑您的问题以显示您尝试了什么,如问题“XML解码响应”中的@bommelding所述。内部XML将被编码。我的答案中的代码是一个文本剪报,只添加了一些换行符。您认为您可以将其读入任何Xml api吗?@bommelding这是因为OP HTML解码了响应。实际响应将具有
?xml version=“1.0”encoding=“Windows-1252”
。是否可以发布soapResponse而不是decodedXml?是否可以发布soapResponse而不是decodedXml?