c# - 如何将 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 并通过。
解决方案
如果您只想知道如何从 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
节点)。
推荐阅读
- mysql - 有没有办法以递归方式查询具有日期范围的日期列?
- android - 如何实例化数据库和我的存储库,应用架构指南
- javascript - 有没有办法重置 JS 事件历史记录,以便我的文件上传器在重置后可以获取相同的文件?
- javascript - 为什么我不应该在 getStaticProps 中进行 api 调用?
- parallel-processing - openmp矩阵乘法
- java - MediaHTTPConnection:尝试在空对象引用上调用虚拟方法“java.net.URLConnection java.net.URL.openConnection()”
- wordpress - 是否可以在第一个用户(用户 A)填写注册表后自动创建第二个用户(用户 B)?
- python - shutil.copytree 到 NAS 在 python 中挂起
- node.js - mongo 文档中的对象在 EJS 中显示未定义
- excel - Excel宏在“,”之后剪切并粘贴到另一个单元格