2019-3-1-C#-json-转-xml-字符串
title | author | date | CreateTime | categories |
---|---|---|---|---|
C# json 转 xml 字符串 |
lindexi |
2019-03-01 09:20:24 +0800 |
2019-1-16 19:5:51 +0800 |
C# |
本文告诉大家如何将 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
博客园博客只做备份,博客发布就不再更新,如果想看最新博客,请访问 https://blog.lindexi.com/
如图片看不见,请在浏览器开启不安全http内容兼容

本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名[林德熙](https://www.cnblogs.com/lindexi)(包含链接:https://www.cnblogs.com/lindexi ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我[联系](mailto:lindexi_gd@163.com)。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?