我有一个.NET项目,它通过HTTP POST以SOAP对象的形式将数据发布到外部Web服务器。这是使用HttpWebRequest对象完成的。我从Web服务器得到一个响应,我用HttpWebResponse对象捕获它。此响应对象也是由SOAP信封包围的XML。
问题是,当我接收响应并将其输出到带有ToString的屏幕时,它显然会核对所有标签,并将它们全部组合成一个字符串。
如何在不删除所有XML格式/标签的情况下从Web服务器输出返回的XML?
以下是我正在使用的代码:
public static XmlDocument PostXMLTransaction(string v_strURL, XmlDocument v_objXMLDoc, string SoapAction, string username, string password)
{
XmlDocument XMLResponse = null;
HttpWebRequest objHttpWebRequest;
HttpWebResponse objHttpWebResponse = null;
Stream objRequestStream = null;
Stream objResponseStream = null;
XmlTextReader objXMLReader;
objHttpWebRequest = (HttpWebRequest)WebRequest.Create(v_strURL);
try
{
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(v_objXMLDoc.InnerXml);
objHttpWebRequest.Method = "POST";
objHttpWebRequest.ContentLength = bytes.Length;
objHttpWebRequest.ContentType = "text/xml; encoding='utf-8'";
objHttpWebRequest.Headers.Add("SOAPAction", "Some URL/"+SoapAction);
NetworkCredential nc = new NetworkCredential(username, password);
objHttpWebRequest.Credentials = nc;
objRequestStream = objHttpWebRequest.GetRequestStream();
objRequestStream.Write(bytes, 0, bytes.Length);
objRequestStream.Close();
objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.GetResponse();
if (objHttpWebResponse.StatusCode == HttpStatusCode.OK)
{
objResponseStream = objHttpWebResponse.GetResponseStream();
objXMLReader = new XmlTextReader(objResponseStream);
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(objXMLReader);
XMLResponse = xmldoc;
objXMLReader.Close();
}
objHttpWebResponse.Close();
}
catch (Exception we)
{
//TODO: Add custom exception handling
throw new Exception(we.Message);
}
finally
{
//Close connections
objRequestStream.Close();
objResponseStream.Close();
objHttpWebResponse.Close();
//Release objects
objXMLReader = null;
objRequestStream = null;
objResponseStream = null;
objHttpWebResponse = null;
objHttpWebRequest = null;
}
//Return
return XMLResponse;
}
我得到的反应如下。 Soap envelop只有xml标签,其他字母带有不同的字符。
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><ns1:getHotelDetail xsi:type="xsd:string" xmlns:ns1="http://axis.frontend.hydra.hotelbeds.com"><HotelDetailRS xmlns="http://www.hotelbeds.com/schemas/2005/06/messages" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.hotelbeds.com/schemas/2005/06/messages HotelDetailRS.xsd" echoToken="DummyEchoToken"><AuditData><ProcessTime>570</ProcessTime><Timestamp>2015-03-20 07:50:07.272</Timestamp><RequestHost>54.169.51.224</RequestHost><ServerName>FORM</ServerName><ServerId>FO</ServerId><SchemaRelease>2005/06</SchemaRelease><HydraCoreRelease>2015.01.14</HydraCoreRelease><HydraEnumerationsRelease>N/A</HydraEnumerationsRelease><MerlinRelease>0</MerlinRelease></AuditData><Hotel xsi:type="ProductHotel"><Code>52317</Code><Name>Hampton Inn &amp; Suites Montreal</Name><DescriptionList><Description type="HotelDescription" languageCode="ENG" languageName="Ingles">where sophistication and culture meet...welcome to the Hampton Inn &amp; Suites by Hilton Montreal (Dorval). At the Hampton Inn &amp; Suites by Hilton&#8482; hotel in Montreal (Dorval), we&apos;re proud of our city. Caf&#233;s, theatres and universities dot this international city, giving Montreal a blend of big-city sophistication and old-world charm. Montreal makes visitors comfortable but also engages them with plenty to do and see. In short, the city remains a delightful blend of old and new. Within just kilometers of the Hampton Inn &amp; Suites by Hilton&#8482; hotel in Montreal (Dorval), you&apos;ll find lovely golf courses, restaurants and shopping. Plus, driving into downtown Montreal takes a mere 15 minutes. The Casino Botanical Gardens and old Montreal are near our hotel in Montreal (Dorval), too. So whether you&apos;re in town to soak up some culture or perhaps see the many metropolitan charms of Montreal, you&apos;ll soon discover that the Hampton Inn &amp; Suites by Hilton hotel in Montreal (Dorval) is where sophistication and culture meet. services &amp; amenities Here at the Hampton Inn &amp; Suites by Hilton hotel in Montreal (Dorval), we&apos;re passionate about taking good care of you. That&apos;s why we offer a broad range of services and amenities to make your stay exceptional. Whether you&apos;re planning a corporate meeting or need accommodations for a family reunion or your child&apos;s sporting group, we&apos;re delighted to offer you easy planning and booking tools to make the process quick and organized.* Meetings &amp; Events * Local Restaurant Guide</Description></DescriptionList><ImageList><Image><Type>GEN</Type><Order>8</Order><Description>General view</Description><Url>http://www.hotelbeds.com/giata/05/052317/052317a_hb_a_008.jpg</Url></Image><Image><Type>GEN</Type><Order>9</Order><Description>General view</Description><Url>http://www.hotelbeds.com/giata/05/052317/052317a_hb_a_009.jpg</Url></Image><Image><Type>GEN</Type><Order>10</Order><Description>General view</Description><Url>http://www.hotelbeds.com/giata/05/052317/052317a_hb_a_010.jpg</Url></Image><Image><Type>GEN</Type><Order>11<
答案 0 :(得分:0)
输出正确,你的响应实际上是一个字符串,而字符串又是xml里面的。 getHotelDetail
响应是一个有效的字符串对象。您可能必须从getHotelDetail字符串内容创建另一个XmlDocument。
我相信这是Web Service内部的东西,其中响应是Xml DataSet而不是XmlDocument。
但是,为什么不简单地从添加服务引用中导入Web服务而不是手动获取Web服务响应,.NET完全支持自动获取和编组对正确对象的响应。