TimHorton
TimHorton

Reputation: 885

Parse nested XML from URL

I'm currently working on a .NET 4.6 console application. I need to parse a nested XML from an URL and transform the XML into an object list.

The URL for the sample XML is the following:

https://www.w3schools.com/xml/cd_catalog.xml

The XML looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<CATALOG>
    <CD>
        <TITLE>Empire Burlesque</TITLE>
        <ARTIST>Bob Dylan</ARTIST>
        <COUNTRY>USA</COUNTRY>
        <COMPANY>Columbia</COMPANY>
        <PRICE>10.90</PRICE>
        <YEAR>1985</YEAR>
    </CD>
    <CD>
        <TITLE>Hide your heart</TITLE>
        <ARTIST>Bonnie Tyler</ARTIST>
        <COUNTRY>UK</COUNTRY>
        <COMPANY>CBS Records</COMPANY>
        <PRICE>9.90</PRICE>
        <YEAR>1988</YEAR>
    </CD>
</CATALOG>

My corresponding C# classes look like this:

[XmlRoot(ElementName = "CATALOG")]
public class Catalog
{
    [XmlElement("CD")]
    List<Cd> Cds { get; set; }
}

[XmlRoot(ElementName = "CD")]
public class Cd
{
    [XmlElement("TITLE")]
    public string Title { get; set; }
    [XmlElement("ARTIST")]
    public string Artist { get; set; }
    [XmlElement("COUNTRY")]
    public string Country { get; set; }
    [XmlElement("COMPANY")]
    public string Company { get; set; }
    [XmlElement("PRICE")]
    public double Price { get; set; }
    [XmlElement("YEAR")]
    public int Year { get; set; }
}

My program class looks like this:

class Program
{
    static void Main(string[] args)
    {
        Init();      
    }

    public static void Init() {

        var url = "https://www.w3schools.com/xml/cd_catalog.xml";

        XmlDocument myXmlDocument = new XmlDocument();
        myXmlDocument.Load(url);

        var catalog = myXmlDocument.InnerXml.ToString();

        var result = Deserialize<Catalog>(catalog);
        // result is null :(

        Console.ReadKey();
    }

    public static T Deserialize<T>(string xmlText)
    {
        try
        {
            var stringReader = new StringReader(xmlText);
            var serializer = new XmlSerializer(typeof(T));
            return (T)serializer.Deserialize(stringReader);
        }
        catch (Exception ex)
        {
            throw;
        }
    }
}

So far so good, my variable catalog consists out of an XML string, but somehow the XML doesn't get parsed correctly. I always get null as a return result. Perhaps it's because of my class definitions.

What do you think, do you have an idea on how to solve this issue? To retrieve an List<Cd> instead of null.

Upvotes: 1

Views: 138

Answers (2)

Munna
Munna

Reputation: 1

List<Cd> Cds { get; set; }

Change this line to

Public List<Cd> Cds { get; set; }

Upvotes: 0

ThePretendProgrammer
ThePretendProgrammer

Reputation: 1517

The error is very subtle. You have done everything right, but you missed to add the public access qualifier in your Catalog class, on the list of Cd like so:

[XmlRoot(ElementName = "CATALOG")]
public class Catalog
{
    [XmlElement("CD")]
    public List<Cd> Cds { get; set; }
}

Since the access qualifiers default to private, the deserializer is having a hard time finding the correct property to deserialize the XML into.

Upvotes: 2

Related Questions