【发布时间】:2020-08-17 18:08:43
【问题描述】:
我有以下 Azure Function c# 代码,可以正常工作。
我调用 web 服务并将 xml 数据作为输入传递,但 Azure 函数得到了 json 数据。
如何在调用 webservice 时映射这个 xml 数据并传递?
string Jsonbody = await req.Content.ReadAsStringAsync();
// I'm confused how to map this Jsonbody data to xml and pass to httpContent
var httpContent = new StringContent(Jsonbody, Encoding.UTF8, "text/xml");
HttpClient httpClient = new HttpClient();
string requestUri = "https://mydemo.com/myservice.asmx?listdata";
var byteArray = Encoding.ASCII.GetBytes("username:password");
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
HttpResponseMessage response = await httpClient.PostAsync(requestUri, httpContent);
string result = await new StreamReader(response.Content.ReadAsStreamAsync().Result).ReadToEndAsync();
例如—— 函数应用输入 json -
{
"Name": "00141169",
"CurrencyCode": "EUR",
"Date": "2020-04-03",
}
映射到这个 xml,该 xml 输入到 webservice 将传递给httpContent
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<listdata xmlns="http://tempuri.org/">
<Name>KH001</Name>
<CurrencyCode>01/01/2018</CurrencyCode>
<Date>01/01/2020</Date>
</listdata>
</soap:Body>
</soap:Envelope>
就像在 Json 中一样,我们使用 C# 类使用 Json 序列化,然后分配值,我们可以用 Xml 做些什么吗?
我还有更多字段,上面 3 个字段只是示例数据。
如果我直接从邮递员调用函数应用程序并将 xml 输入传递给 身体它工作正常。我的问题是如果功能应用程序输入是 json 如何 将其映射到 xml 并传递。
【问题讨论】:
-
你可能需要修改请求是指定你想要xml响应而不是json。
-
通常在调用 asmx 服务时,您会create a service reference。该引用将包含您可以将 json 属性映射到的类和您可以调用的代理,而不是使用
HttpClient。 -
如果我将它作为邮递员的 xml 传递,就像修改后的问题一样,我的问题是如果输入是 json,如何将它映射到 xml 并传递它。
标签: c# json xml azure azure-functions