C#中HttpWebResponse中的XML格式

时间:2015-03-19 12:24:20

标签: c# xml soap

我有一个.NET项目,它通过HTTP POST以SOAP对象的形式将数据发布到外部Web服务器。这是使用HttpWebRequest对象完成的。我从Web服务器得到一个响应,我用HttpWebResponse对象捕获它。此响应对象也是由SOAP信封包围的XML。

问题是,当我接收响应并将其输出到带有ToString的屏幕时,它显然会核对所有标签,并将它们全部组合成一个字符串。

如何在不删除所有XML格式/标签的情况下从Web服务器输出返回的XML?

以下是我正在使用的代码:

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();
    }

我得到的反应如下。 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">&lt;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"&gt;&lt;AuditData&gt;&lt;ProcessTime&gt;570&lt;/ProcessTime&gt;&lt;Timestamp&gt;2015-03-20 07:50:07.272&lt;/Timestamp&gt;&lt;RequestHost&gt;54.169.51.224&lt;/RequestHost&gt;&lt;ServerName&gt;FORM&lt;/ServerName&gt;&lt;ServerId&gt;FO&lt;/ServerId&gt;&lt;SchemaRelease&gt;2005/06&lt;/SchemaRelease&gt;&lt;HydraCoreRelease&gt;2015.01.14&lt;/HydraCoreRelease&gt;&lt;HydraEnumerationsRelease&gt;N/A&lt;/HydraEnumerationsRelease&gt;&lt;MerlinRelease&gt;0&lt;/MerlinRelease&gt;&lt;/AuditData&gt;&lt;Hotel xsi:type="ProductHotel"&gt;&lt;Code&gt;52317&lt;/Code&gt;&lt;Name&gt;Hampton Inn &amp;amp; Suites Montreal&lt;/Name&gt;&lt;DescriptionList&gt;&lt;Description type="HotelDescription" languageCode="ENG" languageName="Ingles"&gt;where sophistication and culture meet...welcome to the Hampton Inn &amp;amp; Suites by Hilton Montreal (Dorval). At the Hampton Inn &amp;amp; Suites by Hilton&amp;#8482; hotel in Montreal (Dorval), we&amp;apos;re proud of our city. Caf&amp;#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;amp; Suites by Hilton&amp;#8482; hotel in Montreal (Dorval), you&amp;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&amp;apos;re in town to soak up some culture or perhaps see the many metropolitan charms of Montreal, you&amp;apos;ll soon discover that the Hampton Inn &amp;amp; Suites by Hilton hotel in Montreal (Dorval) is where sophistication and culture meet. services &amp;amp; amenities Here at the Hampton Inn &amp;amp; Suites by Hilton hotel in Montreal (Dorval), we&amp;apos;re passionate about taking good care of you. That&amp;apos;s why we offer a broad range of services and amenities to make your stay exceptional. Whether you&amp;apos;re planning a corporate meeting or need accommodations for a family reunion or your child&amp;apos;s sporting group, we&amp;apos;re delighted to offer you easy planning and booking tools to make the process quick and organized.* Meetings &amp;amp; Events * Local Restaurant Guide&lt;/Description&gt;&lt;/DescriptionList&gt;&lt;ImageList&gt;&lt;Image&gt;&lt;Type&gt;GEN&lt;/Type&gt;&lt;Order&gt;8&lt;/Order&gt;&lt;Description&gt;General view&lt;/Description&gt;&lt;Url&gt;http://www.hotelbeds.com/giata/05/052317/052317a_hb_a_008.jpg&lt;/Url&gt;&lt;/Image&gt;&lt;Image&gt;&lt;Type&gt;GEN&lt;/Type&gt;&lt;Order&gt;9&lt;/Order&gt;&lt;Description&gt;General view&lt;/Description&gt;&lt;Url&gt;http://www.hotelbeds.com/giata/05/052317/052317a_hb_a_009.jpg&lt;/Url&gt;&lt;/Image&gt;&lt;Image&gt;&lt;Type&gt;GEN&lt;/Type&gt;&lt;Order&gt;10&lt;/Order&gt;&lt;Description&gt;General view&lt;/Description&gt;&lt;Url&gt;http://www.hotelbeds.com/giata/05/052317/052317a_hb_a_010.jpg&lt;/Url&gt;&lt;/Image&gt;&lt;Image&gt;&lt;Type&gt;GEN&lt;/Type&gt;&lt;Order&gt;11&lt;

1 个答案:

答案 0 :(得分:0)

不太清楚你在使用XMLResponse做什么,因为它没有发布,但是为了获取格式化的响应的字符串版本,我会尝试以下方法:

var stringifed = XMLResponse.outerXml;

https://stackoverflow.com/a/13026108/2870218