Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Serialize a Control to JSON?
    primarykey
    data
    text
    <p>I used to store Control information in an XML file, like so:</p> <pre><code>&lt;Controls&gt; &lt;Label Id="heading" Text="This is a heading!" FontStyle="(FontStyleDataHere)" Location="20, 10" /&gt; &lt;Label Id="bodyText" Text="This is Body text." FontStyle="(FontStyleDataHere)" Location="20, 70" /&gt; &lt;/Controls&gt; </code></pre> <p>I have been looking for a printed version of many pages of code that I had last year, and it was the only backup I had left and cannot find it now.</p> <p>And since I cannot remember how the heck I did this, I always fels that XML was incredibly tedious. So I thought, why not give JSON a go. It seems a bit easier...</p> <p>Now, given the above code, I was able to create a class of type Person, and Serialize the object and write it to the file (or Console - whatever):</p> <pre><code>using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.Text; using System.Threading.Tasks; namespace SerializeToJson { class Program { [DataContract] internal class Person { [DataMember] internal String Name; [DataMember] internal Int32 Age; } static void Main(string[] args) { Person person = new Person() { Name = "Jason rules.", Age = 19 }; MemoryStream stream = new MemoryStream(); DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Person)); serializer.WriteObject(stream, person); stream.Position = 0; StreamReader reader = new StreamReader(stream); Console.Write("Json form of Person object: "); Console.WriteLine(reader.ReadToEnd()); Console.ReadKey(); } } } </code></pre> <p>But the problem is, I do not know how to Serialize Controls to Json. And this is what I really need. And, obviously, I will need to then at a later point in time, deserialize them, so they can be re-created at runtime.</p> <p>Can this be done, with JSON, or would you recommend I still use XML for this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload