Using XmlSerializer
From the name you probably guessed that theXmlSerializer
serializes data into XML. It gives you more control over the XML structure of the serialized data. Typical real-world examples for using this serializer would be to maintain compatibility with XML web services. It is also an easy medium to use when transmitting data using some type of message queuing (such as MSMQ or RabbitMQ).
The default behavior of XmlSerializer
is to serialize public fields and properties. Using attributes from the System.Xml.Serialization
namespace, you can control the structure of your XML.
Getting ready
Since we are going to use List<>
in this example, ensure that you have added the using System.Collections.Generic;
namespace. We also want to have more control over the structure of the XML, so also include the using System.Xml.Serialization;
namespace so that we can use the appropriate attributes. Lastly, for the LINQ query, you will need to add the using System.Linq;
namespace.