简体   繁体   English

C#忽略xml反序列化中的父节点

[英]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#. 这是我在这里的第一篇文章,您可以想象,我只是C#的初学者。 So here's my question: I'm deserializing a given xml to a custom class with XmlSerializer . 所以这是我的问题:我正在使用XmlSerializer将给定的xml反序列化为自定义类。

XML: 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: 自定义C#类:

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? 那么是否有可能忽略多数而始终使用单个节点呢?

You could add some helper functions (or properties) to your Program class, eg: 您可以在Program类中添加一些辅助函数(或属性),例如:

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;
            }
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM