0

I am working on an application that parses XML document and serializes it. I am able to parse and convert to JSON but the issue. What I want to achieve is to add an empty array structure in json if that element node is not there in XML. But from my code, the output is coming as element : null.

What I have done so far -

My class:

    [Serializable()]
    [XmlRoot("CarCollection")]
    public class CarCollection
    {
        [XmlArray("Cars")]
        [XmlArrayItem("Car", typeof(Car))]
        public Car[] Car { get; set; }
    }

    [Serializable()]
    public class Car
    {
        [XmlElement("StockNumber")]
        public string stockNumber { get; set; }

        [XmlElement("Make")]
        public string make { get; set; }

        [XmlArray("Models")]
        [XmlArrayItem("model", typeof(Model))]
        public Model[] Model { get; set; }

    }

    [Serializable()]
    public class Model
    {
        [XmlElement("modelName")]
        public string modelName { get; set; }

        [XmlElement("modelType")]
        public string modelType { get; set; }

        [XmlElement("price")]
        public string price { get; set; }

        [XmlElement("preOrderNeeded")]
        public string preOrderNeeded { get; set; }
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<CarCollection>
<Cars>
  <Car>
    <StockNumber>1020</StockNumber>
    <Make>Renault</Make>
    <Models>
       <model>
          <modelName>Kwid</modelName>
          <modelType>Basic</modelType>
          <price>5 Lakhs</price>
          <preOrderNeeded>No</preOrderNeeded>
       </model>
       <model>
          <modelName>Kwid</modelName>
          <modelType>Compact Model with all upgrades</modelType>
          <price>7.25 Lakhs</price>
          <preOrderNeeded>Yes</preOrderNeeded>
       </model>
    </Models>
  </Car>
  <Car>
    <StockNumber>1010</StockNumber>
    <Make>Toyota</Make>

  </Car>
</Cars>
</CarCollection>

My logic for serialization:

CarCollection cars = null;

XmlSerializer serializer = new XmlSerializer(typeof(CarCollection));
using (StreamReader sr = new StreamReader(@"C:\Users\someNAme\Downloads\XML\modelXML.xml"))
{
    cars = (CarCollection)serializer.Deserialize(sr);

    string output = JsonConvert.SerializeObject(cars);
}

This is what I am getting:

{
    "stockNumber": "1010",
    "make": "Toyota",
    "Model": null
}

I want to achieve:

{
    "stockNumber": "1010",
    "make": "Toyota",
    "Model": []
}
ivcubr
  • 1,988
  • 9
  • 20
  • 28
  • 1
    You can just add an extra line of logic, and instantiate a new empty array if the property is null... Or you could just ignore it as mentioned at: https://stackoverflow.com/questions/6507889/how-to-ignore-a-property-in-class-if-null-using-json-net – Hozikimaru Aug 02 '18 at 12:57
  • actually this project is just a sample one. What I am working on is a bit complex. XML nodes as well. So your suggestion might work for straight XMLs . But I tried this also and it is not working. I am expecting something like - "Models": [ { "childElements": { "childElement": [{ }] where childElement is another List inside the model class –  Aug 02 '18 at 13:09
  • Then what is the end goal, you would rather provide an empty object rather than null for the entire schema? – Hozikimaru Aug 02 '18 at 13:11
  • You could also just instantiate the inner arrays as well if you want to see the full structure. Hozikimaru answered the question as per your example though, so it may be good to clarify in your question exactly the structure you want. – Lyco Aug 02 '18 at 20:02

1 Answers1

0

You can try Cinchoo ETL - an open source library, to do the xml -> json conversion as below

using (var r = new ChoXmlReader("Sample49.xml")
    .WithXPath("/CarCollection/Cars/Car")
    )
{
    Console.WriteLine(ChoJSONWriter.ToTextAll(r));
}

Output:

[
 {
  "StockNumber": 1020,
  "Make": "Renault",
  "Models": [
   {
     "modelName": "Kwid",
     "modelType": "Basic",
     "price": "5 Lakhs",
     "preOrderNeeded": "No"
   }
   {
     "modelName": "Kwid",
     "modelType": "Compact Model with all upgrades",
     "price": "7.25 Lakhs",
     "preOrderNeeded": "Yes"
   }
  ]
 },
 {
  "StockNumber": 1010,
  "Make": "Toyota",
  "Models": []
 }
]

Hope it helps.

Cinchoo
  • 6,088
  • 2
  • 19
  • 34