我有一个远程托管的 XML SOAP 消息,需要在 ASP.NET MVC C# Web 应用程序中读取该消息。我对上述所有技术都是新手,所以请放轻松。
- 如何连接到数据源
- 如何创建模型来对 SOAP 消息进行建模
- 我需要什么 LinQ 查询才能将“GetMetalQuoteResult”的内容转换为 C# 对象?例如访问 SOAP 消息响应的各个元素。
下面的架构。
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetMetalQuoteResponse xmlns="http://.../...">
<GetMetalQuoteResult>
<Type>string</Type>
<Currency>string</Currency>
<Date>Date</Date>
<Time>Time</Time>
<Rate>decimal</Rate>
<Bid>decimal</Bid>
<BidTime>Time</BidTime>
<ExpTime>DateTime</ExpTime>
<DisplayTime>Time</DisplayTime>
<DisplayDate>Date</DisplayDate>
<Ask>Decimal</Ask>
<AskTime>Time</AskTime>
</GetMetalQuoteResult>
</GetMetalQuoteResponse>
</soap:Body>
</soap:Envelope>
目前,我的 Controller 中有以下代码。
var xml = XElement.Load(url);
System.Diagnostics.Debug.WriteLine("");
foreach (XElement x in xml.Nodes())
{
System.Diagnostics.Debug.WriteLine(x.Name + ":\n"+ x.Value);
}
System.Diagnostics.Debug.WriteLine("");
但这只会返回以下内容:
{http://schemas.xmlsoap.org/soap/envelope/}
Body:XAUGBP5/22/201212:21:04 PM1000.86251000.862512:21:04 PM2012 May 22 12:21 PM BST1:21:04 PM EDT05/22/121001.249412:21:04 PM
我需要它在单独的行中返回:
Type: XAU
Currency: GBP
Date: 5/22/201212:21:04
....
....
预先感谢您的帮助。
最佳答案
一个好的选择可能是生成客户端代理类。您可以通过在 Visual Studio 中添加服务引用或使用 wsdl.exe 来完成此操作。命令行工具。这样您就可以调用该方法并以常规 C# 对象的形式接收结果,而不必过多担心 SOAP 基础结构。
一旦有了客户端代理,您就可以编写如下所示的代码:
var client = new ServiceReference.ServiceClient();
var result = client.GetMetalQuote();
System.Diagnostics.Debug.WriteLine(result.GetMetalQuoteResult.Currency);
// etc.
关于c# - 读取 SOAP 消息响应 ASP.NET MVC4 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10706979/