首页 > 解决方案 > 如何将 json 数据映射到 xml 以从 azure 函数 c# 调用 webservice

问题描述

我有下面的 Azure Function c# 代码可以正常工作。

我调用 webservice 并将 xml 数据作为输入传递,但 Azure 函数得到了 json 数据。

如何在调用 web 服务时映射此 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 输入到将传递给的 web 服务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 并通过。

标签: c#jsonxmlazureazure-functions

解决方案


如果您只想知道如何从 JSON 字符串反序列化 XML,您可以使用以下代码,在我的测试中,我不会调用其他 Web 服务,我想反序列化是主要问题。

您可以用来JsonConvert进行反序列化,在我的测试中,我将直接返回 XML。

public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

            XNode node = JsonConvert.DeserializeXNode(requestBody);

            return new ContentResult { Content = node.ToString(), ContentType = "application/xml" };
        }

在此处输入图像描述

假设在此之后您可以使用此 XML 调用 Webservice 请求,如果您还有其他问题,请随时告诉我。

更新:我的理解是你想选择一些值或删除一些值,如果是,你可以参考我下面的代码。

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

            XmlDocument doc = JsonConvert.DeserializeXmlNode(requestBody);

            XmlNode rootnode = doc.SelectSingleNode("listdata");

            XmlNode Datenode = rootnode.SelectSingleNode("Date");

            rootnode.RemoveChild(Datenode);


            return new ContentResult { Content = doc.InnerXml.ToString(), ContentType = "application/xml" };

我从请求中获取 json 数据并返回 XML 数据(删除Date节点)。

在此处输入图像描述


推荐阅读