I think you will need namespace "System.Xml.Serialization" to use class XmlSerializer because it has the functionality that you want that will serialize and deserialize to/from XML format to object against known type, like user defined class you will create.
Here is a quick example for you, suppose you have xml file named Person.xml and it is contains these data:
<?xml version="1.0" encoding="utf-8" ?>
<Person>
<Id>3344</Id>
<Name>Ahmed Shaikoun</Name>
<Title>Software Engineer</Title>
<Country>Egypt</Country>
<Phone>0xx-xxxxxxxxx</Phone>
</Person>
And you would like to create console application to just deal with this file to load data from it or to write data to it using built in functionality that support dealing with xml inside .NET framework, all you need first to define class type for this person like this:
public class Person
{
public string Id { get; set;}
public string Name { get; set;}
public string Title { get; set;}
public string Country { get; set;}
public string Phone { get; set;}
public override string ToString()
{
return $"{Name}, {Title}, Id: {Id}, from {Country}";
}
}
Then you will create a new class that will do the serialization/deserialization and parsing of course to load the deserialized object to memory:
public class XMLSerializer
{
public T Deserialize<T>(string input) where T : class
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));
using (StringReader sr = new StringReader(input))
{
return (T)ser.Deserialize(sr);
}
}
public string Serialize<T>(T ObjectToSerialize)
{
XmlSerializer xmlSerializer = new XmlSerializer(ObjectToSerialize.GetType());
using (StringWriter textWriter = new StringWriter())
{
xmlSerializer.Serialize(textWriter, ObjectToSerialize);
return textWriter.ToString();
}
}
}
It is pretty easy, and last you can try it by running this easy example that i made here for you:
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
XMLSerializer serializer = new XMLSerializer();
// load file
string filePath = Directory.GetCurrentDirectory() + @"\Person.xml";
string xmlInputData = File.ReadAllText(filePath);
// deserialize and parse xml to person object
Person person = serializer.Deserialize<Person>(xmlInputData);
// display person object
Console.WriteLine($"Serialize and display person: {person.ToString()}");
// serialize person object to string content
string xmlOutputData = serializer.Serialize<Person>(person);
}
}
This is all you need without dealing manually with data and also it did not require any GUI to build.
And here the full example that I prepared here:
namespace XMLSerialize
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
XMLSerializer serializer = new XMLSerializer();
// load file
string filePath = Directory.GetCurrentDirectory() + @"\Person.xml";
string xmlInputData = File.ReadAllText(filePath);
// deserialize and parse xml to person object
Person person = serializer.Deserialize<Person>(xmlInputData);
// display person object
Console.WriteLine($"Serialize and display person: {person.ToString()}");
// serialize person object to string content
string xmlOutputData = serializer.Serialize<Person>(person);
}
}
public class Person
{
public string Id { get; set;}
public string Name { get; set;}
public string Title { get; set;}
public string Country { get; set;}
public string Phone { get; set;}
public override string ToString()
{
return $"{Name}, {Title}, Id: {Id}, from {Country}";
}
}
public class XMLSerializer
{
public T Deserialize<T>(string input) where T : class
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));
using (StringReader sr = new StringReader(input))
{
return (T)ser.Deserialize(sr);
}
}
public string Serialize<T>(T ObjectToSerialize)
{
XmlSerializer xmlSerializer = new XmlSerializer(ObjectToSerialize.GetType());
using (StringWriter textWriter = new StringWriter())
{
xmlSerializer.Serialize(textWriter, ObjectToSerialize);
return textWriter.ToString();
}
}
}
}
XmlSerializer is defined in Assembly System.Xml.XmlSerializer, it is working with .NET 7 too.
I hope my little answer helping you to do this task.
Thank you