pünktchen
pünktchen

Reputation: 3

C# ignore parent nodes in xml deserialization

this is my first post here and as can you can imagine, i'm just a beginner in c#. So here's my question: I'm deserializing a given xml to a custom class with XmlSerializer.

XML:

<epg>
<program start="20160404234500" stop="20160405001000" channel="281479340566592">
    <eventid>604042345</eventid>
    <titles>
        <title>Schlauberger - Quizzen, was Spaß macht! (1)</title>
    </titles>
    <events>
        <event>27. Schlauberger - Quizzen, was Spaß macht!</event>
    </events>
    <descriptions>
        <description>27. Schlauberger - Quizzen, was Spaß macht!</description>
    </descriptions>
</program>
<program start="20160504234500" stop="20160505001000" channel="281479340566587">
    <eventid>604042348</eventid>
    <title>Schlauberger - Quizzen, was Spaß macht! (2)</title>
    <event>28. Schlauberger - Quizzen, was Spaß macht!</event>
    <description>28. Schlauberger - Quizzen, was Spaß macht!</description>
</program>

Custom C# class:

public class Titles
{
    [XmlElement("title")]
    public string Title { get; set; }
}

public class SubTitles
{
    [XmlElement("event")]
    public string SubTitle { get; set; }
}

public class Descriptions
{
    [XmlElement("description")]
    public string Description { get; set; }
}

public class Program
{
    [XmlElement("eventid")]
    public string EventID { get; set; }
    [XmlElement("titles")]
    public Titles Titles { get; set; }
    [XmlElement("events")]
    public SubTitles SubTitles { get; set; }
    [XmlElement("descriptions")]
    public Descriptions Descriptions { get; set; }
    [XmlAttribute("start")]
    public string Start { get; set; }
    [XmlAttribute("stop")]
    public string Stop { get; set; }
    [XmlAttribute("channel")]
    public string Channel { get; set; }
    [XmlElement("title")]
    public string Title { get; set; }
    [XmlElement("event")]
    public string SubTitle { get; set; }
    [XmlElement("description")]
    public string Description { get; set; }
}

[XmlRoot(ElementName = "epg"), XmlType("epg")]
public class epg
{
    [XmlElement("program")]
    public List<Program> Program { get; set; }
}

No problem so far. But as you can see, because "title", "event" and "description" is sometimes nested in a majority and sometimes not, i can access the properties in my later code sometimes by a list and sometimes directly. This makes the code really inconvenient.
So is it somehow possible to ignore the majorities and always use the single nodes instead?

Upvotes: 0

Views: 479

Answers (1)

ma7r
ma7r

Reputation: 396

You could add some helper functions (or properties) to your Program class, e.g.:

public class Program
    {
        public Titles Titles { get; set; }

        public string Title { get; set; }

        public string GetTitle()
        {
            if (Titles != null)
            {
                return Titles.Title;
            }
            else
            {
                return Title;
            }
        }

    }

Upvotes: 1

Related Questions