none
While converting XML data to JSON RRS feed

  • Question

  • While converting XML data to JSON using c#, By providing the data within the class,I’m getting the output.I need to fetch data from file present in folder .Could anyone please help on this

    using Newtonsoft.Json;
    using System;
    using System.Windows.Forms;
    using System.Xml;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            
            private void Form1_Load(object sender, EventArgs e)
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(" <Student>\r\n    <Student_Name>Preethi</Student_Name>\r\n    <SSN>45679</SSN>\r\n    <Course>Electronics and communication</Course>\r\n    <Address>\r\n      <Line_1>#1, 6th cross</Line_1>\r\n      <Line_2>Victoria Layout</Line_2>\r\n      <City>Bangalore</City>\r\n      <Country>India</Country>\r\n    </Address>\r\n  </Student>\r\n</Root>");
                string jsonText = JsonConvert.SerializeXmlNode(doc);
                //Console.WriteLine(jsonText);
                MessageBox.Show(jsonText);
            }
        }
    }


    • Edited by ShreeKSD Wednesday, March 14, 2018 7:27 PM
    • Edited by KareninstructorMVP, Moderator Wednesday, March 14, 2018 8:11 PM Shorten title, place into post. Place code into code block
    Wednesday, March 14, 2018 7:26 PM

All replies

  • Hello ShreeKSD,

    I don't understand the question.  If it is because of the invalid xml in your example, just remove the invalid Root element at the end.

    If you are asking how to read from a file then the XmlDocument supports the ability to load a file using the Load() method:


    Cheers, Jeff

    Wednesday, March 14, 2018 9:05 PM