C# Httpwebrequest给出了删除所有XML格式/标记的响应
我有一个.NET项目,它通过HTTP POST以SOAP对象的形式将数据发布到外部web服务器。这是使用HttpWebRequest对象完成的。我从web服务器获得响应,我使用HttpWebResponse对象捕获该响应。此响应对象也是由SOAP信封包围的XML 问题是,当我获取响应并使用ToString将其输出到屏幕时,它显然会破坏所有标记,并将其组合成一个字符串 如何在不删除所有XML格式/标记的情况下从web服务器输出返回的XML 以下是我正在使用的代码:C# Httpwebrequest给出了删除所有XML格式/标记的响应,c#,xml,soap,C#,Xml,Soap,我有一个.NET项目,它通过HTTP POST以SOAP对象的形式将数据发布到外部web服务器。这是使用HttpWebRequest对象完成的。我从web服务器获得响应,我使用HttpWebResponse对象捕获该响应。此响应对象也是由SOAP信封包围的XML 问题是,当我获取响应并使用ToString将其输出到屏幕时,它显然会破坏所有标记,并将其组合成一个字符串 如何在不删除所有XML格式/标记的情况下从web服务器输出返回的XML 以下是我正在使用的代码: public static Xm
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;
}
我得到的回应如下。SOAPEnvelope只有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<
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 hoteldertailrs.xsd“echoToken=”Dummeyechotoken“AuditDataProcessTime570/ProcessTimeTimestamp2015-03-20 07:50:07.272/TimestampRequestHost54.169.51.224/RequestHostServerNameFORM/ServerNameServerIdFO/ServerIdSchemaRelease2005/06/SchemaReleaseHydraCoreRelease2015.01.14/HydracoreReleaseHydraEnumerationReleaseN/A/HydraEnumerationReleaseMerlinRelease0/MerlinRelease/AuditDataHotel xsi:type=“ProductHotel”Code52317/CodeNameHampton Inn&;Suites Montreal/NameDescriptionListDescription type=“HotelDescription”languageCode=“ENG”languageName=“Ingles”在这里,成熟与文化交汇……欢迎来到蒙特利尔希尔顿(Dorval)的Hampton Inn&;Suites酒店.蒙特利尔汉普顿希尔顿套房酒店(多瓦尔),我们为我们的城市感到自豪。咖啡馆、剧院和大学点缀着这座国际城市,使蒙特利尔融合了大城市的复杂性和旧世界的魅力。蒙特利尔让游客感到舒适,但也让他们有很多事情要做和看。简言之,这座城市仍然是新旧的令人愉快的融合。仅几公里之内在蒙特利尔(Dorval)希尔顿汉普顿套房酒店(Hampton Inn&;Suites by Hilton&;#8482;hotel)中,您将发现可爱的高尔夫球场、餐厅和购物场所。此外,驱车进入蒙特利尔市中心只需15分钟。赌场植物园和旧蒙特利尔就在我们位于蒙特利尔(Dorval)的酒店附近因此,无论您是来这里领略文化,还是参观蒙特利尔众多的大都市魅力,您都会很快发现蒙特利尔希尔顿酒店(Dorval)的汉普顿套房酒店(Hampton Inn&;Suites by Hilton hotel)这里是成熟和文化的交汇处。蒙特利尔(多瓦尔)希尔顿汉普顿酒店和套房的服务和设施,我们热情地照顾您。这就是为什么我们提供广泛的服务和便利设施,使您的住宿与众不同。无论您是在计划公司会议,还是在家庭团聚时需要住宿,还是您孩子的体育团体,我们都很高兴为您提供轻松的计划和预订工具,以使流程快速有序。*会议和活动*本地餐厅指南/说明/描述ListImageListImageTypeGen/TypeOrder8/OrderDescriptionGeneral view/DescriptionUrlhttp://www.hotelbeds.com/giata/05/052317/052317a_hb_a_008.jpg/Url/ImageImageTypeGEN/TypeOrder9/OrderDescriptionGeneral 查看/说明Urlhttp://www.hotelbeds.com/giata/05/052317/052317a_hb_a_009.jpg/Url/ImageImageTypeGEN/TypeOrder10/OrderDescriptionGeneral 查看/描述rlhttp://www.hotelbeds.com/giata/05/052317/052317a_hb_a_010.jpg/Url/ImageImageTypeGEN/TypeOrder11
输出是正确的,您的响应实际上是一个字符串,其中包含xml。getHotelDetail
响应是一个有效的字符串对象。您可能需要从getHotelDetail字符串内容创建另一个XmlDocument
我相信这是Web服务内部的东西,响应是Xml数据集而不是XmlDocument
但是,为什么不直接从添加服务引用导入Web服务,而不是手动获取Web服务响应呢?.NET完全支持自动获取和封送响应到适当的对象