none
Master-/Detail as XML or JSON RRS feed

  • Question

  • Hello,
    What options do I have?
    To save and load it  inside a FILE. 
       - XML
       - JSON JavaScript Object Notation (JSON)
      
    What should I do exactly?
    Can someone give me an example?
    I think this variant is faster than a dataset.

    For JSON, I try to use a library from Newtonsoft, it is not working. t1, data is empty.
    Maybe Microsoft has his own library.
    string t1 = JsonConvert.SerializeObject(masters);
    	var data = JsonConvert.SerializeObject(masters, Newtonsoft.Json.Formatting.Indented); //, JsonSerializerSettings) ;
    
    
    	protected static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings
    	{
    		NullValueHandling = NullValueHandling.Ignore
    	};
       
    
    static void Main(string[] args)
    {
    	try
    	{
    		Demo c = new Demo();
    		c.Execute();
    	}
    	catch (Exception ex) { Console.WriteLine(ex.ToString()); }
    	Console.WriteLine("Fertig, Abschluss mit beliebiger Taste");
    	Console.ReadKey();
    }
    
    internal class Demo
    {
    	internal void Execute()
    	{
    		List<Master> masters = new List<Master>();
    		for (int i = 1; i < 10; i++)
    			masters.Add(new Master() { ID = i, Info = $"Master {i}" });
    		
    		List<Child> childs = new List<Child>();
    		for (int i = 1; i < 100; i++)
    			childs.Add(new Child() { ID = i, FK = 1 + i % 9, Info = $"Child {i}" });
    	
    	}
    }
    
    internal class Master
    {
    	internal int ID { get; set; }
    	internal string Info { get; set; }
    }
    internal class Child
    {
    	internal int ID { get; set; }
    	internal int FK { get; set; }
    	internal string Info { get; set; }
    }

    With best regards Markus
    Friday, May 25, 2018 4:31 PM

Answers

All replies

  • The code you posted does not compile, of course.  Were those first two lines supposed to be inside Demo.Execute?  Perhaps you should post a program that does work.

    Microsoft does have the DataContractJsonSerializer, but there's nothing wrong with the library you've chosen.  It should work fine.

    The decision on what format to choose needs to be based on what you expect to do with the files.  If this is a file that will only be used by your program, never read by humans and never used by anyone else, then there's nothing wrong with the BinaryFormatter.  If you want to be able to edit the files yourself, or if you expect to transfer them to other machines or other programs, then either XML or JSON makes sense.  XML has the added advantage that you can specify the data format very precisely with an XSL file.

    At this point, you should not worry about performance.  Serialization and deserialization are operations that happen very rarely.  It doesn't matter if they're slow.


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Friday, May 25, 2018 5:13 PM
  • Regardless of using XML or JSON, the children should be implemented within the parent.

    public class Master
    {
      public int ID { get; set; }
      public string Info { get; set; }
      public List<Child> { get; set; }
    }
    
    public class Child
    {
    	public int ID { get; set; }
    	public int FK { get; set; }
    	public string Info { get; set; }
    }

    Friday, May 25, 2018 6:43 PM
  • Hi,

    Can you give me an example to save this in XML JSON file and read again? What's the procedure. Thank you in advance.

    With best regards Markus

    Saturday, May 26, 2018 10:43 AM
  • http://blog.danskingdom.com/saving-and-loading-a-c-objects-data-to-an-xml-json-or-binary-file/
    Saturday, May 26, 2018 11:06 AM
  • Your question is not phrased correctly.  You would either use XML or JSON.  There's no such thing as an "XML JSON file".

    You already have the JsonConvert code in your program; that can serialize into a file and deserialize from a file.  There are samples on the NewtonSoft web site.  With that, there's no need to involve XML.


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Sunday, May 27, 2018 6:34 AM