Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON to XML class conversions

Tags:

json

c#

class

xml

linq

I cannot seem to line up the class of the Json into Linq XML.

The c.first, c.second and the c.third are highlighted and states:

"Are you missing a using directive or assembly reference."

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 =>
        new XElement("place",
            new XElement("first", c.first),
            new XElement("second", c.second),
            new XElement("third", c.third)
        )
    ))
);

Class.cs

public class jClass
{
    public jNumber[] count { get; set; }
}
public class jNumber
{
    public jTopThree[] place { get; set; }
}
public class jTopThree
{
    public int first { get; set; }
    public int second { get; set; }
    public int third { get; set; }
}
like image 534
Filoso Avatar asked Mar 28 '26 23:03

Filoso


1 Answers

Your problem is that your object structure is essentially an array of array of arrays and you're only doing one Select. Where you're building your xml, your c variable is at the jClass level, so you're trying to read the first, second and third properties from that.

It's unclear what your xml structure should be, but you're either going to need to use a couple more .Select calls to drill down further to the jTopThree instances, use .SelectMany to flatten it out, or change your object definitions up a bit.

like image 199
Mike Goatly Avatar answered Mar 31 '26 12:03

Mike Goatly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!