Parse XML in .net framework 4.0

Peter Volz 1,295 Reputation points
2023-05-23T09:17:07.7333333+00:00

Hello all

We have this 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>

How to parse it to get 3 parts out of it?

  1. elementCount as Integer
  2. OPFCategoryCopyName as String
  3. OPFCategoryCopyBackgroundColor as RGB Drawing.Color

No idea where and how to start, never worked with XML parsers (just did one basic task)

Developer technologies VB
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Nisha Pillai 75 Reputation points Microsoft Employee
    2023-05-23T10:57:29.2933333+00:00

    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

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Dewayne Basnett 1,381 Reputation points
    2023-05-23T13:19:49.5833333+00:00

    In VB

            Dim someXML As XElement
            ' someXML = XElement.Load("PATH HERE")
    
            'for testing use literal
            someXML = <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>
    
            Dim count As Integer = Integer.Parse(someXML.@elementCount)
            For Each el As XElement In someXML.Elements
                Dim name As String = el.@OPFCategoryCopyName
                Dim color As Drawing.Color
                Dim cps() As String = el.@OPFCategoryCopyBackgroundColor.Split()
                'at this point cps has three values e.g.
                ' r:0.62 
                ' g:0.40 
                ' b:0.20
                ' not clear what color format specification this is 
                ' so not sure how to proceed
                Stop
            Next
    
    
    

    I've never seen a color spec like the one in the XML


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.