While converting XML data into JSON using C#, parent tag is missing


#1

Hi,

I have code to convert the XML data to Json using C#. The problem here parent tag is not displaying in the output. It looks like line feed(LF) populated instead of CRLF. Below is the program which is used and the out which I get and out how i want. Could anyone please advise on this.

var xml =
@“
Shruthi</student Name>
45678
Electronics and communication

<Line 1=”">#1, 6th cross</Line 1>
<Line 2="">Victoria Layout</Line 2>
Bangalore</City
India



Preethi</student Name>
45679
Electronics and communication

<Line 1="">#1, 6th cross</Line 1>
<Line 2="">Victoria Layout</Line 2>
Bangalore
India

";

C# Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Script.Serialization;

namespace ConsoleApp1
{
class Program

{
    static void Main()
    {
        try
        {
            var xml =
            @"<Root>
Shruthi 45678 Electronics and communication #1, 6th cross Victoria Layout Bangalore Preethi 45679 Electronics and communication #1, 6th cross Victoria Layout Bangalore India ";
            XDocument doc = XDocument.Parse(xml);
            Dictionary<string, string> dataDictionary = new Dictionary<string, string>();

            string JsonData = "";


            foreach (XElement element in doc.Descendants().Where(p => p.HasElements == false))
            {
                int keyInt = 0;
                string keyName = element.Name.LocalName;

                if (dataDictionary.ContainsKey(keyName))
                {
                   
                    var jsonText = new JavaScriptSerializer().Serialize(dataDictionary);
                    JsonData += jsonText;
                    dataDictionary = new Dictionary<string, string>();
                }

                dataDictionary.Add(keyName, element.Value);
               
            }

            if (dataDictionary.Count != 0)
            {
                var jsonText = new JavaScriptSerializer().Serialize(dataDictionary);
                JsonData += jsonText;
            }

            //string jsonT = JsonConvert.SerializeXmlNode(doc);

            /*var dic = XDocument
                .Parse(xml)
                .Descendants("Student")
                .ToDictionary(
                    c => c.Attribute("Name").Value,
                    c => c.Value
                );*/

            var json = new JavaScriptSerializer().Serialize(dataDictionary);
            Console.WriteLine(json);

        }
        catch
        { }
    }
   
    
}

}

Expected output:

“Student”: {
“Student_Name”: “Shruthi”,
“SSN”: “45678”,
“Course”: “Electronics and communication”,

  "Address": {
                    "Line_1": "#1, 6th cross",
                   "Line_2": " Victoria Layout",
                "City": " Bangalore",
               "Country": " India"}
                "Student": {
                    "Student_Name": "Preethi",
  "SSN": "45679",

“Course”: “Electronics and communication”,

  "Address": {
                        "Line_1": "#1, 6th cross",
                   "Line_2": " Victoria Layout",
                "City": " Bangalore",
               "Country": " India"}

Present output:

{
“Student_name”:“Shruthi”,
“SSN”:“45678”,
“Course”:“Electronics and communication”,
“Line_1”:"#1, 6th cross",
“Line_2”:“Victoria Layout”,
“City”:“Bangalore”,
“Country”:“India”
}

{
“Student_name”:“Preethi”,
“SSN”:“45679”,
“Course”:“Electronics and communication”,
“Line_1”:"#1, 6th cross",
“Line_2”:“Victoria Layout”,
“City”:“Bangalore”,
“Country”:“India”
}