C# json 转 xml 字符串

栏目: ASP.NET · 发布时间: 6年前

内容简介:本文告诉大家如何将 json 转 xml 或将 xml 转 json 字符串首先需要安装 Newtonsoft.Json 库,打开 VisualStudio 2019 新建一个 dotnet core 项目,然后右击编译 csproj 输入下面的代码尝试创建一个类用来转换为 xml 请看代码

本文告诉大家如何将 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>
  <Blog>https://blog.csdn.net/lindexi_gd</Blog>
</Foo>
转换json
{"?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
<?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>

Converting between JSON and XML

代码 https://gitee.com/lindexi/lindexi_gd/tree/dev/LapouRairpaltearwou


以上所述就是小编给大家介绍的《C# json 转 xml 字符串》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

以匠心,致设计:网易 UEDC 用户体验设计

以匠心,致设计:网易 UEDC 用户体验设计

网易用户体验设计中心 / 电子工业出版社 / 2018-8 / 95.00元

为什么网易云音乐的体验流畅细腻、富有温度?为什么网易严选App的UI设计让人感到温馨、舒适?为什么网易蜗牛读书App的界面设计让用户爱不释手…… 《以匠心,致设计:网易 UEDC 用户体验设计》是网易用户体验设计团队对自身过去的设计思考的精心总结,最大程度还原了网易产品背后的设计故事,内容涵盖设计基础、设计实践、方法思考、成长指南四章,借助网易产品设计的实际案例具体讲述了设计师日常工作中不可......一起来看看 《以匠心,致设计:网易 UEDC 用户体验设计》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具