3

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; }
}
user3079834
  • 2,009
  • 2
  • 31
  • 63
Filoso
  • 41
  • 3
  • `c` is of the type `jClass` which only contains the property `count` which is an array of `jNumber`. Which value were you actually trying to pick? 1? 22? 333? – Equalsk Mar 31 '17 at 08:13
  • How do you equate the jTopThree so all the firsts,seconds and thirds in the xml document being created? – Filoso Mar 31 '17 at 08:20
  • Never mind, I figured out what your JSON should look like. Do you have an example of the desired XML? – Equalsk Mar 31 '17 at 08:38

4 Answers4

2

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.

Mike Goatly
  • 7,380
  • 2
  • 32
  • 33
  • I need two more ".Select" They need tiered down for each class heading What do you put in front of second and third ".Select" to represent the respective classes? – Filoso Mar 31 '17 at 08:27
  • Do you have an example of the XML that you're trying to output? – Mike Goatly Mar 31 '17 at 08:31
0

json text and object structure seems to be invalid. you can try some like this..

object structure

public class jClass
{
    public jTopThree[] count { get; set; }
}
public class jTopThree
{
    public int first { get; set; }
    public int second { get; set; }
    public int third { get; set; }
}

serialization

        var serializer = new JavaScriptSerializer();
        var json1 = "{count:[{first:1,second:2,third:3},{first:11,second:22,third:33},{first:111,second:222,third:333}]}";
        var obj = serializer.Deserialize<jClass>(json1);
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(jClass));
        string xmlString;
        using (System.IO.StringWriter sww = new System.IO.StringWriter())
        {
            using (XmlWriter writer = XmlWriter.Create(sww))
            {
                xmlSerializer.Serialize(writer, obj);
                xmlString = sww.ToString();
            }
        }
levent
  • 3,464
  • 1
  • 12
  • 22
0
        var json1 = "[{\"place\":{\"first\":1,\"second\":2,\"third\":3}},{\"place\":{\"first\":11,\"second\":22,\"third\":33}},{\"place\":{\"first\":111,\"second\":222,\"third\":333}}]";
        XmlDocument deserializeXmlNode;
        if (json1.IndexOf("[", StringComparison.OrdinalIgnoreCase) == 0)
        {
            deserializeXmlNode = Newtonsoft.Json.JsonConvert.DeserializeXmlNode($"{{\"items\":{json1}}}", "source");
        }
        else
        {
            deserializeXmlNode = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json1, "source");
        }

        Console.WriteLine(deserializeXmlNode.OuterXml);

If you want just copy to XML and can afford Nugget package, go for Newtonsoft.Json and modify the code above to suite your needs. I have slighlty changed the input json, because it was not valid per http://www.json.org/ . It should work with any valid JSON though.

http://www.newtonsoft.com/json/help/html/ConvertingJSONandXML.htm

How to convert JSON to XML or XML to JSON?

Community
  • 1
  • 1
Bobo
  • 335
  • 2
  • 10
0

So firstly your JSON is formatted badly, based on your example I think it should look like this (to me having "place" seems redundant but I've left it for now):

{
"count": [
{"place": [
{"first":1,"second":2,"third":3}]},
{"place": [
{"first":11,"second":22,"third":33}]},
{"place": [
{"first":111,"second":222,"third":333}]}
]
}

Your classes should look like this:

public class RootObject
{
    public List<Count> count { get; set; }
}

public class Count
{
    public List<Place> place { get; set; }
}

public class Place
{
    public int first { get; set; }
    public int second { get; set; }
    public int third { get; set; }
}

Lastly you can create XML this way:

var serializer = new JavaScriptSerializer();
var json = "your json string";
var result= serializer.Deserialize<RootObject>(json);
var xmld = new XDocument(
           new XElement("count", result.count
               .SelectMany(p => p.place)
               .Select(x => 
                   new XElement("place",
                       new XElement("first", x.first),
                       new XElement("second", x.second),
                       new XElement("third", x.third)
                   )
                ))
           );

Output:

<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>
Equalsk
  • 7,954
  • 2
  • 41
  • 67