相关文章推荐
酷酷的围巾  ·  Any example of how to ...·  2 月前    · 
深沉的牙膏  ·  vb.net 日期转换成字符串函数 ...·  1 年前    · 
痴情的高山  ·  上海市血液中心联动柯南 / ...·  1 年前    · 
深沉的苹果  ·  贪心算法解决tsp问题的实验原理 - CSDN文库·  1 年前    · 
闯红灯的松球  ·  别看我只是一只羊_百度百科·  1 年前    · 
Code  ›  C# json 转 xml 字符串开发者社区
string xml语言 xml转json
https://cloud.tencent.com/developer/article/2065809
至今单身的牛排
2 年前
作者头像
林德熙
0 篇文章

C# json 转 xml 字符串

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > 林德熙的博客 > C# json 转 xml 字符串

C# json 转 xml 字符串

作者头像
林德熙
发布 于 2022-08-04 13:34:25
753 0
发布 于 2022-08-04 13:34:25
举报

本文告诉大家如何将 json 转 xml 或将 xml 转 json 字符串

首先需要安装 Newtonsoft.Json 库,打开 VisualStudio 2019 新建一个 dotnet core 项目,然后右击编译 csproj 输入下面的代码

  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
  </ItemGroup>

尝试创建一个类用来转换为 xml 请看代码

    public class Foo
        public string Name { get; set; }
        public string Blog { get; set; }
    }

将类转换为 xml 的代码

            var foo = new Foo()
                Name = "lindexi",
                Blog = "https://blog.csdn.net/lindexi_gd",
            var xmlSerializer = new XmlSerializer(typeof(Foo));
            var str = new StringBuilder();
            xmlSerializer.Serialize(new StringWriter(str), foo);
            var xml = str.ToString();
            Console.WriteLine(xml);

现在运行就可以看到下面代码

<?xml version="1.0" encoding="utf-16"?>
<Foo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Name>lindexi</Name>
  <Blog>https://blog.csdn.net/lindexi_gd</Blog>
</Foo>

这里的 encoding 是 utf-16 因为 StringWriter 使用的是 Unicode 如果需要修改为 utf-8 需要修改代码,但是本文就不在这里说

xml 转 json 字符串

从 xml 转 json 需要将 xml 字符串创建 XmlDocument 才可以

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

通过下面代码就可以将 XmlDocument 转 json 字符串

            string text = JsonConvert.SerializeXmlNode(doc);

运行代码可以看到转换的代码

{"?xml":{"@version":"1.0","@encoding":"utf-16"},"Foo":{"@xmlns:xsd":"http://www.w3.org/2001/XMLSchema","@xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance","Name":"lindexi","Blog":"https://blog.csdn.net/lindexi_gd"}}

json 转 xml 字符串

在上面已经转换出 json 可以通过下面代码将 json 转 xml 字符串

            doc = (XmlDocument) JsonConvert.DeserializeXmlNode(text);

如果需要将 doc 做字符串输出,可以使用 doc.InnerXml 转字符串

            doc = (XmlDocument) JsonConvert.DeserializeXmlNode(text);
            Console.WriteLine("json转xml");
            Console.WriteLine(doc.InnerXml);

运行软件可以看到下面代码

<?xml version="1.0" encoding="utf-16"?><Foo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Name>lindexi</Name><Blog>https://blog.csdn.net/lindexi_gd</Blog></Foo>

下面是全部的代码

   class Program
        static void Main(string[] args)
            var foo = new Foo()
                Name = "lindexi",
                Blog = "https://blog.csdn.net/lindexi_gd",
            var xmlSerializer = new XmlSerializer(typeof(Foo));
            var str = new StringBuilder();
            xmlSerializer.Serialize(new StringWriter(str), foo);
            var xml = str.ToString();
            Console.WriteLine(xml);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            string text = JsonConvert.SerializeXmlNode(doc);
            Console.WriteLine("转换json");
            Console.WriteLine(text);
            doc = (XmlDocument) JsonConvert.DeserializeXmlNode(text);
            Console.WriteLine("json转xml");
            Console.WriteLine(doc.InnerXml);
            Console.Read();
    public class Foo
        public string Name { get; set; }
        public string Blog { get; set; }
    }

运行可以看到下面方法

<?xml version="1.0" encoding="utf-16"?>
<Foo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Name>lindexi</Name>
 
推荐文章
酷酷的围巾  ·  Any example of how to use the Dom API? - ReScript Forum
2 月前
深沉的牙膏  ·  vb.net 日期转换成字符串函数 vb将日期转为字符串_huatechinfo的技术博客_51CTO博客
1 年前
痴情的高山  ·  上海市血液中心联动柯南 / 《无颜之月》重制版海报公开|贺图|心跳回忆|成人动画|名侦探柯南|恋爱冒险游戏_网易订阅
1 年前
深沉的苹果  ·  贪心算法解决tsp问题的实验原理 - CSDN文库
1 年前
闯红灯的松球  ·  别看我只是一只羊_百度百科
1 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号