Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Adaptive dialog HttpRequest中从xml转换为json?_C#_Json_Xml_Botframework_Chatbot - Fatal编程技术网

C# 如何在Adaptive dialog HttpRequest中从xml转换为json?

C# 如何在Adaptive dialog HttpRequest中从xml转换为json?,c#,json,xml,botframework,chatbot,C#,Json,Xml,Botframework,Chatbot,我不熟悉Bot框架和C#。我正在使用带有核心航班预订模板()的自适应对话框构建聊天机器人。我想通过API调用获取天气信息。这个OpenWeatherAPI可以返回JSON、XML或HTML格式的数据。当响应是JSON格式时,很容易访问键值对。但当响应是XML格式时,所有内容都将转换为字符串并保存为响应中的“内容”,我必须将其转换为JSON或字典,以便访问详细信息。我需要将API响应保存为对话框中的属性,以供以后参考 我知道我们可以使用以下代码将XML转换为JSON,但问题是如何在adaptive

我不熟悉Bot框架和C#。我正在使用带有核心航班预订模板()的自适应对话框构建聊天机器人。我想通过API调用获取天气信息。这个OpenWeatherAPI可以返回JSON、XML或HTML格式的数据。当响应是JSON格式时,很容易访问键值对。但当响应是XML格式时,所有内容都将转换为字符串并保存为响应中的“内容”,我必须将其转换为JSON或字典,以便访问详细信息。我需要将API响应保存为对话框中的属性,以供以后参考

我知道我们可以使用以下代码将XML转换为JSON,但问题是如何在adaptive对话框中进行转换。我试图在HttpRequest块中包含以下代码,但出现错误“XmlDocument是一种类型,在给定的上下文中无效”。似乎您无法在自适应对话框中添加自己的自定义代码,您所能做的只是使用模板所能提供的,但是现在HttpRequest类没有解析XML响应的选项。有人能给我一些指导吗?谢谢

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

string json = JsonConvert.SerializeXmlNode(doc);
在自适应对话框中生成HttpRequest的代码:

new IfCondition()
{
    Condition = "conversation.Id != null",
    Actions = new List<Dialog>()
    {
        new HttpRequest()
        {
            Url = "http://api.openweathermap.org/data/2.5/weatherq=Detroit&mode=xml&appid=appid={your api key}",
            ResultProperty = "dialog.httpResponse",
            Method = HttpRequest.HttpMethod.GET,
            ResponseType = HttpRequest.ResponseTypes.Json
        },
        new Send Activity("${dialog.httpResponse}"),
        new Send Activity("${dialog.httpResponse.content}")
    }
}
newifcondition()
{
Condition=“conversation.Id!=null”,
操作=新列表()
{
新的HttpRequest()
{
Url=”http://api.openweathermap.org/data/2.5/weatherq=Detroit&mode=xml&appid=appid={您的api密钥}”,
ResultProperty=“dialog.httpResponse”,
方法=HttpRequest.HttpMethod.GET,
ResponseType=HttpRequest.ResponseType.Json
},
新建发送活动(“${dialog.httpResponse}”),
新的发送活动(“${dialog.httpResponse.content}”)
}
}
下面显示了HttpRequest响应的外观。OpenWeather API响应(XML格式)被转换为字符串作为“内容”的值

{
    "statusCode": 200,
    "reasonPhrase": "OK",
    "headers": 
    {
        "Server": "openresty",
        "Date": "Tue, 14 Jul 2020 18:57:41 GMT",
        "Connection": "keep-alive",
        "X-Cache-Key": "/data/2.5/weather?mode=xml&q=detroit",
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Credentials": "true",
        "Access-Control-Allow-Methods": "GET, POST"
    },
    "content": "<?xml version="1.0" encoding="UTF-8"?>\n
                <current>
                    <city id="4990729" name="Detroit">
                        <coord lon="-83.05" lat="42.33"></coord> 
                        <country>US</country>
                        <timezone>-14400</timezone>
                        <sun rise="2020-07-14T10:08:16" set="2020-07-15T01:07:33"></sun>
                    </city>
                    <temperature value="301.11" min="300.15" max="302.59" unit="kelvin"></temperature>
                    <feels_like value="301.1" unit="kelvin"></feels_like> 
                    <humidity value="44" unit="%"></humidity>
                    <pressure value="1019" unit="hPa"></pressure>
                    <wind>
                        <speed value="2.1" unit="m/s" name="Light breeze"></speed>
                        <gusts></gusts>
                        <direction></direction>
                    </wind>
                    <clouds value="75" name="broken clouds"></clouds>
                    <visibility value="16093"></visibility>
                    <precipitation mode="no"></precipitation>
                    <weather number="803" value="broken clouds" icon="04d"></weather>
                    <lastupdate value="2020-07-14T18:57:41"></lastupdate> 
               </current>"
}
{
“状态代码”:200,
“合理短语”:“好的”,
“标题”:
{
“服务器”:“openresty”,
“日期”:“2020年7月14日星期二18:57:41 GMT”,
“连接”:“保持活动状态”,
“X-Cache-Key”:“/data/2.5/weather?mode=xml&q=detroit”,
“访问控制允许来源”:“*”,
“访问控制允许凭据”:“true”,
“访问控制允许方法”:“获取、发布”
},
“内容”:“\n
美国
-14400
"
}

您可以使用代码操作

new IfCondition()
{
    Condition = "conversation.Id != null",
    Actions = new List<Dialog>()
    {
        new HttpRequest()
        {
            Url = "http://api.openweathermap.org/data/2.5/weatherq=Detroit&mode=xml&appid=appid={your api key}",
            ResultProperty = "dialog.httpResponse",
            Method = HttpRequest.HttpMethod.GET,
            ResponseType = HttpRequest.ResponseTypes.Json
        },
        new CodeAction(async (dc, options) =>
        {
            var xml = dc.State.GetValue("dialog.httpResponse.content", () => "<root></root>");
            var doc = new XmlDocument();

            doc.LoadXml(xml);

            var json = JsonConvert.SerializeXmlNode(doc);

            dc.State.SetValue("dialog.json", json);

            return await dc.EndDialogAsync();
        }),
        new SendActivity("${dialog.json}"),
    }
}
newifcondition()
{
Condition=“conversation.Id!=null”,
操作=新列表()
{
新的HttpRequest()
{
Url=”http://api.openweathermap.org/data/2.5/weatherq=Detroit&mode=xml&appid=appid={您的api密钥}”,
ResultProperty=“dialog.httpResponse”,
方法=HttpRequest.HttpMethod.GET,
ResponseType=HttpRequest.ResponseType.Json
},
新的CodeAction(异步(dc,选项)=>
{
var xml=dc.State.GetValue(“dialog.httpResponse.content”,()=>”;
var doc=新的XmlDocument();
doc.LoadXml(xml);
var json=JsonConvert.SerializeXmlNode(doc);
SetValue(“dialog.json”,json);
return wait dc.EndDialogAsync();
}),
新建SendActivity(“${dialog.json}”),
}
}

欢迎来到堆栈溢出。您是否愿意编辑您的帖子,使您的JSON成为格式化的代码块而不是屏幕截图?请看一下方便的指南,看看你可以采取哪些步骤更快地得到更好的答案:谢谢你的评论,@KyleDelaney。我已经根据你的建议更新了这个问题,JSON响应的屏幕截图被一个格式化的代码块所取代。你说“这个OpenWeather API可以返回JSON、XML或HTML格式的数据。”那么,你为什么不先获取JSON格式的数据呢?你能链接到API文档吗?谢谢你的回复,凯尔。实际上,我只是以OpenWeatherAPI为例,出于隐私考虑,我不能透露我需要调用的实际API。对于实际的API,只有XML格式的响应,否则我可以直接使用JSON响应。谢谢,谢谢你,凯尔!我试图使用
CodeAction
,但不知道如何访问自定义代码中的
对话框.httpResponse
。您提供的上述代码非常有效。我又添加了一行来解析json,这样就可以直接从
dialog.json
访问键值对。在
var json=JsonConvert.SerializeXmlNode(doc)之后,我添加了
var json\u final=JObject.Parse(json)
然后将
json\u final
设置为
dialog.json
的值,以便可以使用
“${dialog.json.current.weather}”
获取天气信息。