从XMLDocument中读取XML / SOAP

时间:2009-09-09 21:27:02

标签: .net xml soap

我假设您可以以某种方式使用XmlReader从XmlDocument中读取信封。有没有人有一个很好的例子如何?我想简单地将整个发送的SOAP文本提取到一个字符串变量中,以便我们可以在发生请求错误时将其记录下来。

示例方法:

protected virtual HttpWebRequest CreateWebRequest(string endPoint, Int32 contentLength)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_requestURI);
    request.Method = "POST";
    request.ContentLength = contentLength;
    request.ContentType = "text/xml;charset=UTF-8";

    return request;
}


public Response.Response SendRequest()
{
    XmlDocument doc = CreateRequestXML();

    // Determins if API call needs to use a session based URI
    string requestURI = UseAuthURI ? _requestURIAuthBased + sID : _requestURI;

    byte[] data = XmlUtil.DocumentToBytes(doc);

    // Create the atual Request instance
    HttpWebRequest request = CreateWebRequest(requestURI, data.Length);
    request.ContentLength = data.Length;
    request.KeepAlive = false;
    request.Timeout = 30000;

    // Send the Request
    using (Stream requestStream = request.GetRequestStream())
    {
        requestStream.Write(data, 0, data.Length);
    }

    // get response
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        if (response.StatusCode != HttpStatusCode.OK)
        {
            string message = String.Format("POST failed. Received HTTP {0}", response.StatusCode);
            throw new Exception(message);
        }

        using (Stream responseStream = response.GetResponseStream())
        {
            using (XmlReader reader = XmlReader.Create(responseStream))
            {
                return CreateResponse(reader);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

使用doc.OuterXML工作。