Json手动转xml格式

匹诺丶老曹 2019-06-17 04:04:13
{
"Contents": [{
"Text": " 下面哪个关于苹果的故事是真的 ",
"Type": 0
}, {
"ImageFileName": "D:\\git\\QuestionsImporter\\QuestionsImporter\\bin\\Debug\\VirtualDbFolder\\13739770-5e08-4158-af36-219af27eb872.jpg",
"Type": 1
}, {
"ImageFileName": "D:\\git\\QuestionsImporter\\QuestionsImporter\\bin\\Debug\\VirtualDbFolder\\6e0aae18-e04a-4571-998a-c025c596791b.jpg",
"Type": 1
}, {
"Text": "",
"Type": 0
}]
}

以上是Jsoon字符串,我想手动转换为xml格式的宗富川
...全文
169 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
正怒月神 2019-06-18
  • 打赏
  • 举报
回复
JsonConvert.DeserializeXmlNode; JsonConvert.DeserializeXNode; 你看看哪个方法适合你
wanghui0380 2019-06-18
  • 打赏
  • 举报
回复
至于你最后写的那个是什么?我来告诉你,那个叫 XSLT(xml样式转换文件)
wanghui0380 2019-06-18
  • 打赏
  • 举报
回复
反序列化成对象------在序列化成xml 话说你别觉着这是弯路,世界上很多事情就这样。 2进制 转 任意进制 ----------------2 转 10,10转任意 把人民币转成欧元----------------美元是本位,人民币对欧元的汇率,是转美元,美元到欧元 把中国的两,转成欧美的盎司----------两转克,克转盎司
匹诺丶老曹 2019-06-18
  • 打赏
  • 举报
回复
谢谢大家的帮助
匹诺丶老曹 2019-06-18
  • 打赏
  • 举报
回复
我这样写了,手动拼接的 xml = "<div>"; JavaScriptSerializer ser = new JavaScriptSerializer(); Dictionary<string, List<Dictionary<string, string>>> JsonText = ser.Deserialize<Dictionary<string, List<Dictionary<string, string>>>>(Json); if(JsonText == null || JsonText.Count <=0) { return false; } else { List<Dictionary<string, string>> JsonContents = JsonText["Contents"] as List<Dictionary<string, string>>; foreach(var JC in JsonContents) { foreach(var v in JC) { if(v.Key == "Text") { //文字 xml += "<p dir=\"ltr\" class=\"pt - a1\">"; xml += "<span xml:space=\"preserve\" class=\"pt - a2\"> </span>"; xml += "<span lang=\"zh - CN\" class=\"pt - a2\">" + v.Value + "</span>"; xml += "</p>"; } else if(v.Key == "ImageFileName") { //图片 xml += "<p dir=\"ltr\" class=\"pt - a1\">"; xml += "<img src=\"" + v.Value + "\" xmlns = \"\" /> "; xml += "<span xml:space=\"preserve\" class=\"pt - 000000\"> </span>"; xml += "</p>"; } } } } xml += "</div>";
kampoo 2019-06-17
  • 打赏
  • 举报
回复
使用json.net:
string json = @"{
'@Id': 1,
'Email': 'james@example.com',
'Active': true,
'CreatedDate': '2013-01-20T00:00:00Z',
'Roles': [
'User',
'Admin'
],
'Team': {
'@Id': 2,
'Name': 'Software Developers',
'Description': 'Creators of fine software products and services.'
}
}";

XNode node = JsonConvert.DeserializeXNode(json, "Root");

Console.WriteLine(node.ToString());
// <Root Id="1">
// <Email>james@example.com</Email>
// <Active>true</Active>
// <CreatedDate>2013-01-20T00:00:00Z</CreatedDate>
// <Roles>User</Roles>
// <Roles>Admin</Roles>
// <Team Id="2">
// <Name>Software Developers</Name>
// <Description>Creators of fine software products and services.</Description>
// </Team>
// </Root>
enaking 2019-06-17
  • 打赏
  • 举报
回复
网上有很多Json的类,读出来,然后写入xml文件。
  • 打赏
  • 举报
回复
class XXX{
public List<Contents> Contents{get;set;
}
class YYY
{
public string Text{get;set;}
public int Type{get;set;}
public string ImageFileName{get;set;}
}

<root>
<Contents>
   <YYY>
      <Text>下面哪个关于苹果的故事是真的</Text>
      <Type>0</Type>
   </YYY>
<Contents>
</root>
it_gz_xi 2019-06-17
  • 打赏
  • 举报
回复
下个Json的DLL,里面好像直接有方法可以转
  • 打赏
  • 举报
回复
建议转实体类先,再做处理。

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧