我在从wcf消息中检索正文时遇到了一些麻烦.我正在尝试实现WCF消息检查器来验证针对XSD架构的消息.
肥皂体如下:
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Header xmlns="http://www.test1.com">
<applicationID>1234</applicationID>
</Header>
<GetMatchRequest xmlns="http://www.tempuri.org">test</GetMatchRequest>
</s:Body>
Run Code Online (Sandbox Code Playgroud)
问题是,当我尝试获取身体时,它只会得到部分身体信息.获取只有header元素,忽略GetMatchRequest元素(可能是因为多个名称空间...)
我使用以下来获取邮件正文:
XmlDocument bodyDoc = new XmlDocument();
bodyDoc.Load( message.GetReaderAtBodyContents().ReadSubtree());
Run Code Online (Sandbox Code Playgroud)
我也试过以下:
bodyDoc.Load( message.GetReaderAtBodyContents());
Run Code Online (Sandbox Code Playgroud)
上面的代码导致错误 - 该文档已经有一个'DocumentElement'节点.
任何人都可以帮助从WCF消息中提取身体吗?
谢谢
Message.GetReaderAtBodyContents返回不在元素处但位于其第一个子元素的读取器.通常,消息正文只包含一个根元素,因此您可以直接加载它.但是在你的消息中它包含多个根元素(Header和GetMatchRequest),所以如果你想在XmlDocument中加载整个主体,你需要提供一个包装元素(XmlDocument只能有一个根元素).在下面的例子中,我<s:Body>用作包装元素,但你可以使用你想要的任何东西.代码只是读取正文,直到找到结束元素(</s:Body>).
public class Post_a866abd2_bdc2_4d30_8bbc_2ce46df38dc4
{
public static void Test()
{
string xml = @"<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
<s:Body xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<Header xmlns=""http://www.test1.com"">
<applicationID>1234</applicationID>
</Header>
<GetMatchRequest xmlns=""http://www.tempuri.org"">test</GetMatchRequest>
</s:Body>
</s:Envelope>";
Message message = Message.CreateMessage(XmlReader.Create(new StringReader(xml)), int.MaxValue, MessageVersion.Soap11);
Console.WriteLine(message);
XmlDocument bodyDoc = new XmlDocument();
MemoryStream ms = new MemoryStream();
XmlWriter w = XmlWriter.Create(ms, new XmlWriterSettings { Indent = true, IndentChars = " ", OmitXmlDeclaration = true });
XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents();
w.WriteStartElement("s", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
while (bodyReader.NodeType != XmlNodeType.EndElement && bodyReader.LocalName != "Body" && bodyReader.NamespaceURI != "http://schemas.xmlsoap.org/soap/envelope/")
{
if (bodyReader.NodeType != XmlNodeType.Whitespace)
{
w.WriteNode(bodyReader, true);
}
else
{
bodyReader.Read(); // ignore whitespace; maintain if you want
}
}
w.WriteEndElement();
w.Flush();
Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
ms.Position = 0;
XmlDocument doc = new XmlDocument();
doc.Load(ms);
Console.WriteLine(doc.DocumentElement.OuterXml);
}
}
Run Code Online (Sandbox Code Playgroud)