【发布时间】:2016-12-08 08:06:39
【问题描述】:
我需要从我的 WCF 消息中获取一个值。我的消息在调试器中具有以下值:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IBrokerService/SaveAndPrint</Action>
</s:Header>
<s:Body>
<SaveAndPrint xmlns="http://tempuri.org/">
<contract xmlns:d4p1="http://schemas.datacontract.org/2004/07/BrokerService.Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<d4p1:ContainerHistoryContracts i:nil="true" />
<!--Lots of nodes removed for brevity-->
<d4p1:CurrentBagId>123456</d4p1:CurrentBagId>
<!--Lots more nodes removed for brevity-->
<d4p1:WorkStation>TheNeededValue</d4p1:WorkStation>
</contract>
</SaveAndPrint>
</s:Body>
</s:Envelope>
但尽我所能,我无法使用 XmlDictionaryReader 获取 d4p1:WorkStation 值。任何人都知道如何使用 XmlDictionaryReader 做到这一点?
注意:我尝试使用TypedMessageConverter,但生成的类没有MessageContract 的属性(尽管它们确实有DataContract)
更新:我所拥有的不起作用。但是如果你想看的话,这里是:
// Load the message into an xml doc
var navigator = buffer.CreateNavigator();
MemoryStream memoryStream = new MemoryStream();
XmlWriter xmlWriter = XmlWriter.Create(memoryStream);
navigator.WriteSubtree(xmlWriter);
xmlWriter.Flush();
xmlWriter.Close();
memoryStream.Position = 0;
XDocument xdoc = XDocument.Load(XmlReader.Create(memoryStream));
var workstationElement =
xdoc.Descendants(XName.Get("StringValue",
@"/s:Envelope[@xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/""]/s:Body/SaveAndPrint[@xmlns=""http://tempuri.org/""]/contract[@xmlns:d4p1=""http://schemas.datacontract.org/2004/07/BrokerService.Contracts""]/d4p1:WorkStation"));
【问题讨论】:
-
你的 XML 解析代码是什么样的?
-
@DVK - 不确定我损坏的代码会有什么帮助,但我已经添加了它。