如何使用LINQ将JSON转换为XML [英] How do I convert JSON to XML using LINQ

查看:91
本文介绍了如何使用LINQ将JSON转换为XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用LINQ将JSON转换为XML。我不想使用NewtonSoft jso.net。

  var  serializer =  new  JavaScriptSerializer(); 
var json1 = ['count' :[ '地点':{ '第一': '1', '第二': '2', '第三': '3'}],[ '地点':{ '第一个': '11', '第二' :'22' , '第三': '33'}],[ '地点':{ '第一': '111', '第二': '222', '第三': '333'}]]]} ;
var jsons = serializer.Serialize(json1);
var jsona = serializer.Deserialize< List< jClass>>(jsons);
var xmld = new XDocument(
new XElement( count,jsona.Select(c = > c)
.SelectMany(p = > p.place)
。选择(x = >
new XElement( place
new XElement( first,x.first),
new XElement ( second,x.second),
新的 XElement( third,x.third)

) )
);



Class.cs

  public   class  jClass 
{
public jCount [] count {获取; set ; }
}
public class jCount
{
public jPlace [] place { get ; set ; }
}
public class jPlace
{
public int first { get ; set ; }
public int second { get ; set ; }
public int third { get ; set ; }
}



应该如下所示:

 <   count  >  
< 地点 >
< 首先 > 1 < / first >
< second > 2 < / second >
< third > 3 < / third >
< / place >
< 地点 >
< first > 11 < / first >
< second > 22 < / second >
< third > 33 < span class =code-keyword>< / third >
< / place >
< 地点 >
< first > 111 < / first >
< second > 222 < / second >
< third > 333 < / third >
< / place >
< / count >





我的尝试:



我尝试了很多不同的Linq将json转换为xml。错误发生在p.place。

解决方案

Quote:

jsona 。选择(c => c)



需要:

 jsona.SelectMany(c => c。计数)


I am trying to convert JSON to XML using LINQ. I want to not use NewtonSoft jso.net.

var serializer = new JavaScriptSerializer();
            var json1 = "['count':['place':{'first':'1','second':'2','third':'3'}],['place':{'first':'11','second':'22','third':'33'}],['place':{'first':'111','second':'222','third':'333'}]]]}";
            var jsons = serializer.Serialize(json1);
            var jsona = serializer.Deserialize<List<jClass>>(jsons);
            var xmld = new XDocument(
                new XElement("count",  jsona.Select(c => c)
               .SelectMany(p => p.place)
               .Select(x =>
                   new XElement("place",
                        new XElement("first", x.first),
                        new XElement("second", x.second),
                        new XElement("third", x.third)
                    )
                ))
            );


Class.cs

public class  jClass
    {
        public jCount[] count { get; set; }
    }
    public class jCount
    {
        public jPlace[] place { get; set; }
    }
    public class jPlace
    {
        public int first { get; set; }
        public int second { get; set; }
        public int third { get; set; }
    }


Should look like this:

<count>
  <place>
    <first>1</first>
    <second>2</second>
    <third>3</third>
  </place>
  <place>
    <first>11</first>
    <second>22</second>
    <third>33</third>
  </place>
  <place>
    <first>111</first>
    <second>222</second>
    <third>333</third>
  </place>
</count>



What I have tried:

I have tried many different Linq to convert json to xml. The error is at "p.place".

解决方案

Quote:

jsona.Select(c => c)


That needs to be:

jsona.SelectMany(c => c.count)


这篇关于如何使用LINQ将JSON转换为XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆