如何编辑SOAP请求正文



我使用Visual Studio添加了一个web服务,并编写了以下代码:

ReceiverClient rc = new ReceiverClient();


var task = rc.sendDocumentAsync("arg1", "arg2", "arg3");


var resutl = task.Result;

因此它创建了请求:

<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<s:Header>
<a:Action>sendDocument</a:Action>
<transportHeader xmlns="http://example.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<authInfo>
<clientEntityId>clientId</clientEntityId>
</authInfo>
</transportHeader>
<a:MessageID>messageId</a:MessageID>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
<a:To></a:To>
<wsse:Security>
</wsse:Security>
</s:Header>
<s:Body xmlns:d2p1="http://docs.oasis-open.org/wss/2004/02/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" d2p1:Id="BodyID-6b6c35aa-a174-4b47-a1bf-bafa61efed78">
<sendDocument xmlns="http://example.com">
<oid xmlns="">oid</oid>
<service xmlns="">service</service>
<document xmlns="">document</document>
</sendDocument>
</s:Body>
</s:Envelope>

我需要用真实id替换clientId,有没有办法以某种方式编辑请求正文?

使用Xml Linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication193
{
class Program
{
const string FILENAME = @"c:temptest.xml";
static void Main(string[] args)
{
string xml = File.ReadAllText(FILENAME);
XDocument doc = XDocument.Parse(xml);
XElement id = doc.Descendants().Where(x => x.Name.LocalName == "clientEntityId").FirstOrDefault();
id.SetValue("123");

}
}
}

最新更新