Deserialize Text file using C# and VB.Net in Windows Application

Last Reply 11 months ago By pandeyism

Posted 11 months ago

Hello,

ever since I put "using (var stream = File.Create(path_combined)){}" code line, my deserialization stoped working.

When I re-run the program the data is not fetched from json txt (in txt it updates successfully)

I don't use streamline to write or read, do you have any suggestions?

    [Serializable]
    public partial class Form1 : Form
    {
        //bool var = Class1.flag;
        //FILENAME
        string input = Interaction.InputBox("Enter a serial number", "TEST", "Default", -1, -1);
        //DEFAULT PATH ROOT
        String root = @".\\";

        Class1 c1 = new Class1();//holds method's attribute flags
        Class1 c2 = new Class1(); //holds method flags whether the used or not

        string path_combined;
        public Form1()
        {
            InitializeComponent();
            input += ".txt";
            //default path + new filename
            path_combined = Path.Combine(root, input);

            if (!File.Exists(path_combined))
            {
                //File.Create(path_combined);
                using (var stream = File.Create(path_combined))
                {
                    if (new FileInfo(path_combined).Length > 0)
                    {
                        //flag situation
                        string json2 = File.ReadAllText(path_combined);
                        FormatJson(json2);
                        c1 = JsonConvert.DeserializeObject<Class1>(json2);

                        //Method used or not
                        string json = File.ReadAllText(path_combined);
                        FormatJson(json);
                        c2 = JsonConvert.DeserializeObject<Class1>(json);
                    }
                }
            }
        }

        private static string FormatJson(string json)
        {
            dynamic parsedJson = JsonConvert.DeserializeObject(json);
            return JsonConvert.SerializeObject(parsedJson, Formatting.Indented);
        }

        // SET TRUE
        private void Button1_Click(object sender, EventArgs e)
        {
            c1.flag = true;
            c1.flag2 = true;
            c2.M1 = true;

            string json = JsonConvert.SerializeObject(c1, Formatting.Indented);
            File.WriteAllText(path_combined, json);

            string json2 = JsonConvert.SerializeObject(c2, Formatting.Indented);
            File.WriteAllText(path_combined, json2);
        }

 

Posted 11 months ago

Hi Dan97

 Refer below sample.

Namespaces

C#

using System.IO;
using Newtonsoft.Json;

VB.Net

Imports System.IO
Imports Newtonsoft.Json

Code

C#

private void Form1_Load(object sender, EventArgs e)
{
    string readJson = new StreamReader(@"C:\Users\anand\Desktop\JsonData.txt").ReadToEnd();

    // Serialize Json Data.
    string serializeData = FormatJson(readJson);
    File.WriteAllText(@"C:\Users\anand\Desktop\Test.txt", serializeData);

    // Deserialize Json Data.
    List<Customers> customer = JsonConvert.DeserializeObject<List<Customers>>(readJson);
    dataGridView1.DataSource = customer;
}

private static string FormatJson(string json)
{
    string jsonData = JsonConvert.SerializeObject(json);
    return jsonData;
}

public class Customers
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
}

VB.Net

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Dim readJson As String = New StreamReader("C:\Users\anand\Desktop\JsonData.txt").ReadToEnd()
        Dim serializeData As String = FormatJson(readJson)
        File.WriteAllText("C:\Users\anand\Desktop\Test.txt", serializeData)
        Dim customer As List(Of Customers) = JsonConvert.DeserializeObject(Of List(Of Customers))(readJson)
        dataGridView1.DataSource = customer
    End Sub

    Private Shared Function FormatJson(ByVal json As String) As String
        Dim jsonData As String = JsonConvert.SerializeObject(json)
        Return jsonData
    End Function

    Public Class Customers
        Public Property Id As Integer
        Public Property Name As String
        Public Property Country As String
    End Class

Screenshot