Hi @Peter Volz
You can use either use XmlSerializer or XmlReader to Parse XML documents.
XML Serializer --> https://learn.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlserializer?view=netframework-4.8.1
XML Reader --> https://learn.microsoft.com/en-us/dotnet/standard/linq/stream-xml-fragments-xmlreader
Here is a sample using XML Serializer.
using System;
using System.Drawing;
using System.IO;
using System.Xml.Serialization;
[XmlRoot("categories")]
public class Categories
{
[XmlAttribute("elementCount")]
public int ElementCount { get; set; }
[XmlElement("category")]
public Category[] CategoryList { get; set; }
}
public class Category
{
[XmlAttribute("OPFCategoryCopyName")]
public string Name { get; set; }
[XmlAttribute("OPFCategoryCopyBackgroundColor")]
public string BackgroundColor { get; set; }
}
class Program
{
static void Main()
{
string xml = @"<?xml version=""1.0""?>
<categories elementCount=""8"">
<category OPFCategoryCopyName=""Family"" OPFCategoryCopyBackgroundColor=""r:0.63 g:0.26 b:0.80"" xml:space=""preserve""/>
<category OPFCategoryCopyName=""Friends"" OPFCategoryCopyBackgroundColor=""r:0.40 g:0.27 b:0.80"" xml:space=""preserve""/>
<category OPFCategoryCopyName=""Holiday"" OPFCategoryCopyBackgroundColor=""r:0.89 g:0.15 b:0.15"" xml:space=""preserve""/>
<category OPFCategoryCopyName=""Manager"" OPFCategoryCopyBackgroundColor=""r:0.85 g:0.42 b:0.72"" xml:space=""preserve""/>
<category OPFCategoryCopyName=""Networking"" OPFCategoryCopyBackgroundColor=""r:0.90 g:0.40 b:0.11"" xml:space=""preserve""/>
<category OPFCategoryCopyName=""Personal"" OPFCategoryCopyBackgroundColor=""r:0.26 g:0.60 b:0.00"" xml:space=""preserve""/>
<category OPFCategoryCopyName=""Team"" OPFCategoryCopyBackgroundColor=""r:0.31 g:0.49 b:0.80"" xml:space=""preserve""/>
<category OPFCategoryCopyName=""Travel"" OPFCategoryCopyBackgroundColor=""r:0.62 g:0.40 b:0.20"" xml:space=""preserve""/>
</categories>";
XmlSerializer serializer = new XmlSerializer(typeof(Categories));
Categories categories;
using (StringReader reader = new StringReader(xml))
{
categories = (Categories)serializer.Deserialize(reader);
Console.WriteLine("elementCount: " + categories.ElementCount);
Console.WriteLine();
foreach (Category category in categories.CategoryList)
{
Console.WriteLine("OPFCategoryCopyName: " + category.Name);
Console.WriteLine("OPFCategoryCopyBackgroundColor: " + ParseColor(category.BackgroundColor).ToString());
Console.WriteLine();
}
}
Console.ReadLine();
}
static Color ParseColor(string colorString)
{
string[] components = colorString.Split(' ');
float red = float.Parse(components[0].Substring(2));
float green = float.Parse(components[1].Substring(2));
float blue = float.Parse(components[2].Substring(2));
return Color.FromArgb((int)(red * 255), (int)(green * 255), (int)(blue * 255));
}
}
Hope this helps.
Kind Regards,
Nisha