Deserializing Objects from XML in C#

Here's another C# code snippet that takes me way too much time to recreate by just reading the documentation.

This is a simple example of a class that can be serialized to/from XML. In this case the "ServerConfig" XML string can contain a list of servers, looking like this:

<ServerConfig loggingEnabled="1">
    <Server host="" port="9999" />
    <Server host="" port="8888" />

The client code can just do "var serverConfig = ServerConfig.FromXmlString(s);" to deserialize it into a ServerConfig object.

(The method that would serialize a ServerConfig to an XML string is left as an exercise for the reader. I rarely need to do that.)

© 2003-2023 Kristopher Johnson