If I have an XML such as the following:
<?xml version="1.0" encoding="UTF-8" ?>
<Config>
<Interface>
<Theme>Dark</Theme>
<Mode>Advanced</Mode>
</Interface>
<Export>
<Destination>\\server1.example.com</Destination>
<Destination>\\server2.example.com</Destination>
<Destination>\\server3.example.com</Destination>
</Export>
</Config>
I can easily deserialize the XML and get element values in the "Interface" section by the following method:
using System;
using System.IO;
using System.Xml.Serialization;
static void Main(string[] args)
{
var serializer = new XmlSerializer(typeof(Config));
using (var stream = new FileStream(@"C:\Temp\Config.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
{
var config = (Config)serializer.Deserialize(stream);
Console.WriteLine($"Theme: {config.Interface.Theme}");
Console.WriteLine($"Mode: {config.Interface.Mode}");
Console.ReadKey();
}
}
[Serializable, XmlRoot("Config")]
public class Config
{
public Interface Interface { get; set; }
}
public struct Interface
{
public string Theme { get; set; }
public string Mode { get; set; }
}
How should I deserialize the array of "Destination" elements in the "Export" section, such that I can loop through an array object to print the values?
i.e.
foreach (destination d in export)
{
Console.WriteLine(destination);
}
cs public class Export { public List<string> Destination { get; set; } } public class Config { public Interface Interface { get; set; } public Export Export { get; set; } }Console.WriteLine(config.Export.Destination[0]);