相关文章推荐
文质彬彬的大象  ·  QML 缩放 —— ...·  7 月前    · 
暗恋学妹的柳树  ·  什么是样板代码?_问答-阿里云开发者社区·  1 年前    · 
英俊的刺猬  ·  WPF Button ...·  1 年前    · 
私奔的毛巾  ·  使用python第三方库计算某一天距元旦的天 ...·  1 年前    · 
彷徨的铁链  ·  go中的time的RFC3339中的格式化 ...·  1 年前    · 
Code  ›  C# json 转 xml 字符串开发者社区
string xml语言 xml转json
https://cloud.tencent.com/developer/article/2065809
至今单身的牛排
1 年前
作者头像
林德熙
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>
 
推荐文章
文质彬彬的大象  ·  QML 缩放 —— 不同设备的适配显示解决方案 - 简书
7 月前
暗恋学妹的柳树  ·  什么是样板代码?_问答-阿里云开发者社区
1 年前
英俊的刺猬  ·  WPF Button 的IsEnable绑定到多个属性 - 只吃肉不喝酒 - 博客园
1 年前
私奔的毛巾  ·  使用python第三方库计算某一天距元旦的天数博-腾讯云开发者社区-腾讯云
1 年前
彷徨的铁链  ·  go中的time的RFC3339中的格式化 - 哆啦在这A梦在哪 - 简书
1 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号