【问题标题】:Web api return custom json format but how to convert to xml in c#Web api返回自定义json格式但如何在c#中转换为xml
【发布时间】:2017-08-14 07:36:26
【问题描述】:

我需要帮助。如何将自定义 json 格式转换为 xml。我的json格式是

{
  status : true;
  Code : 200;
  data : [
           {
             name :ram;
             age : 21;
           }
         ]
}

如何在c#中改变XML格式

【问题讨论】:

    标签: c# json xml


    【解决方案1】:

    使用 Newtonsoft.Json

    http://www.newtonsoft.com/json/help/html/ConvertJsonToXml.htm

    该库允许您在 json 和 xml 之间进行转换。从上面的页面:

    using System;
    using Newtonsoft.Json;
    using System.Xml.Linq;
    using System.Xml;
    
    public class Program
    {
        public static void Main()
        {
            string json = @"{
                              'status': 'true',
                              'Code': '200',
                              'data': [
                                {
                                  'name': 'ram',
                                  'age': '21'
                                }
                              ]
                            }";
            XNode node = JsonConvert.DeserializeXNode(json, "Root");
            Console.WriteLine(node.ToString());
        }
    }
    

    输出:

    <Root>
      <status>true</status>
      <Code>200</Code>
      <data>
        <name>ram</name>
        <age>21</age>
      </data>
    </Root>
    

    DotNetFiddle 查看一个工作示例:https://dotnetfiddle.net/20vf4W

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      • 2016-12-20
      • 2020-08-30
      相关资源
      最近更新 更多