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
2019-3-1-C#-json-转-xml-字符串的更多相关文章
- C# json 转 xml 字符串
本文告诉大家如何将 json 转 xml 或将 xml 转 json 字符串 首先需要安装 Newtonsoft.Json 库,打开 VisualStudio 2019 新建一个 dotnet cor ...
- 使用Visual Studio 快速把 Json,Xml 字符串创建为一个实体类
- Beego 输出数据格式JSON、XML、JSONP
JSON.XML.JSONP beego 当初设计的时候就考虑了 API 功能的设计,而我们在设计 API 的时候经常是输出 JSON 或者 XML 数据,那么 beego 提供了这样的方式直接输出: ...
- js压缩xml字符串,将xml字符串转换为xml对象,将xml对象转换为json对象
/** * 压缩xml字符串 */ function compressXmlStr(str){ var prefix, suffix; var i = str.indexOf("\r&quo ...
- Java JSON、XML文件/字符串与Bean对象互转解析
前言 在做web或者其他项目中,JSON与XML格式的数据是大家经常会碰见的2种.在与各种平台做数据对接的时候,JSON与XML格式也是基本的数据传递格式,本文主要简单的介绍JSON/XML ...
- 字符串json转换为xml xml转换json
原文:字符串json转换为xml xml转换json // To convert an XML node contained in string xml into a JSON string XmlD ...
- xml字符串转xml对象,xml对象转json对象
xml字符串转xml对象: function loadXml(str) { if (str == null) { return null; } var doc = str; try{ doc = cr ...
- xml字符串转json字符串
XML字符串转JSON字符串网上的方法很多,这里主要推荐两种: 1.使用org.json包 jar地址:http://mvnrepository.com/artifact/org.json/json ...
- .NET Core采用的全新配置系统[6]: 深入了解三种针对文件(JSON、XML与INI)的配置源
物理文件是我们最常用到的原始配置的载体,最佳的配置文件格式主要由三种,它们分别是JSON.XML和INI,对应的配置源类型分别是JsonConfigurationSource.XmlConfigura ...
- iOS - 分析JSON、XML的区别和解析方式的底层是如何实现的(延伸实现原理)
<分析JSON.XML的区别,JSON.XML解析方式的底层是如何实现的(延伸实现原理)> (一)JSON与XML的区别: (1)可读性方面:基本相同,XML的可读性比较好: (2)可扩展 ...
随机推荐
- A标签跳转链接并修改样式
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- WWDC2014代码和视频下载
WWDC2014 sample code 地址 http://pan.baidu.com/s/1qWGznnY WWDC2014 videos 地址 https://github.com/liubin ...
- 前端每日实战:138# 视频演示如何用纯 CSS 创作一张 iPhone 价格信息图
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/OorLGZ 可交互视频 此视频是可 ...
- fedora18 [linux]Error: failure: repodata/repomd.xml from fedora: [Errno 256] No more mirrors to try.
在使用fedora17 系统的yum源的时候出现了如下错误: Error: failure: repodata/repomd.xml from fedora: [Errno 256] No more ...
- 用Python给头像加上圣诞帽或圣诞老人小图标
随着圣诞的到来,想给给自己的头像加上一顶圣诞帽.如果不是头像,就加一个圣诞老人陪伴. 用Python给头像加上圣诞帽,看了下大概也都是来自2017年大神的文章:https://zhuanlan.z ...
- jQuery实现三级联动菜单(鼠标悬停联动)
效果图: 代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> < ...
- Stream 源码分析
Stream 支持顺序和并行聚合操作的一组元素序列. 1)operations:支持在单个元素上执行的操作,流操作分为中间操作和终止操作 1-1)中间操作: 1-1-1)无状态:unordered() ...
- hive三种方式区别和搭建
Hive 中 metastore(元数据存储)的三种方式: a)内嵌 Derby 方式 b)Local 方式 c)Remote 方式 第一种方式 ...
- Python学习笔记(20)-文件和文件夹的移动、复制、删除、重命名
一,概述 python中对文件和文件夹进行移动.复制.删除.重命名,主要依赖os模块和shutil模块,要死记硬背这两个模块的方法还是比较困难的,可以用一个例子集中演示文件的移动.复制.删除.重命名, ...
- Fiddler抓百度请求
fiddler是一个很好的抓包工具,默认是抓http请求的,对于pc上的https请求,会提示网页不安全,这时候需要在浏览器上安装证书. 一.网页不安全 1.用fiddler抓包时候,打开百度网页:h ...