Serialising and Deserialising C# code to Xml | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Serialising and Deserialising C# code to Xml

Hey there! I First of all, thank you for all the help on my "Classes" question. They helped a lot. Now i need to know how to serialize/deserialize a class from C# code, to Xml. Please note that i am working on a console app on .NetCore NOT in windows forms. I have already created a class with some properties, an empty constructor, and i called an instance of it in my main program. So how do i convert that to Xml, and where do i find the Xml file? I will include some code below. Thanks! //using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel; //using System.Data; //using System.DirectoryServices; //using System.DirectoryServices.AccountManagement; using System.IO; using System.IO.Compression; using System.Runtime.Serialization.Formatters.Binary; using System.Security; using System.Security.Cryptography; using System.Text; using System.Xml; using System.Xml.Linq; using System.Xml.Schema; using System.Xml.Serialization; namespace SerializeDesirialise { class Program { public class TestClass { public TestClass() { } public string name { get; set; } public int age { get; set; } public string gender { get; set; } } static void Main(string[] args) { TestClass Person = new TestClass(); Person.name = "Andrew"; Person.age = 5; Person.gender = "male"; } } }

15th Jun 2017, 6:50 AM
Andrew Bratu
Andrew Bratu - avatar
4 Answers
+ 1
https://www.dotnetperls.com/serialize-list Have a look at this web-page Mark you class with the serialize attriubte [Serializable()] Use a formatter to serialize the object https://www.codeproject.com/Articles/483055/XML-Serialization-and-Deserialization-Part This is also a nice project. Which does not use the serializable object but uses a other method.
16th Jun 2017, 11:20 AM
sneeze
sneeze - avatar
0
What is the purpose of serialisation. Save the current state of the object?
15th Jun 2017, 2:16 PM
sneeze
sneeze - avatar
0
For now, there is no purose for me. I just need to see the xml code
15th Jun 2017, 2:30 PM
Andrew Bratu
Andrew Bratu - avatar
0
Thank you!
16th Jun 2017, 11:20 AM
Andrew Bratu
Andrew Bratu - avatar