0

I want to read the following xml file format:

<?xml version="1.0" encoding="ISO-8859-1"?>
<WIDECAST_DVB>
  <channel name="XXX">
    <event id="0" start_time="2015-01-27 14:00:00" duration="28800">
      <short_event_descriptor lang="alb" name="Edicion informativ">Lajme nga vendi dhe bota</short_event_descriptor>
      <extended_event_descriptor lang="alb">
        <text />
      </extended_event_descriptor>
    </event>
    <event id="1" start_time="2015-01-27 22:00:00" duration="28800">
      <short_event_descriptor lang="alb" name="Edicion informativ">Lajme nga vendi dhe bota</short_event_descriptor>
      <extended_event_descriptor lang="alb">
        <text>jchdvdgd 
        </text>
      </extended_event_descriptor>
    </event>
    <event id="2" start_time="2015-01-28 06:00:00" duration="28800">
      <short_event_descriptor lang="alb" name="Edicion informativ">Lajme nga vendi dhe bota</short_event_descriptor>
      <extended_event_descriptor lang="alb">
        <text />
      </extended_event_descriptor>
    </event>
    <event id="3" start_time="2015-01-28 14:00:00" duration="28800">
      <short_event_descriptor lang="alb" name="Edicion informativ">Lajme nga vendi dhe bota</short_event_descriptor>
      <extended_event_descriptor lang="alb">
        <text />
      </extended_event_descriptor>
    </event>
 </channel>
</WIDECAST_DVB>

I want to read this fields:

name, start_time, duration, short event descriptor_name, short event description (text within tags), extended_event_descriptor/text (text within text tag)

my class is:

 public class epg2
    {
        public epg2()
        {
            EventNumber = new List<Event>();
        }
        virtual public string channelname { get; set; }
        virtual public List<Event> EventNumber { get; set; }
        virtual public int number { get; set; }

    }
    public class Event
    {
        public Event()
        {

        }
        virtual public DateTime starttime { get; set; }
        virtual public int duration { get; set; }
        virtual public string name { get; set; }
        virtual public string shortDescription { get; set; }
        virtual public string longDescription { get; set; }

    }
Maria Agaci
  • 281
  • 2
  • 3
  • 11
  • You posted an XML but not a single line of code!? Have you tried to read it? For more info, read: http://stackoverflow.com/questions/4752796/how-to-read-xml-in-net – Kenan Zahirovic Feb 16 '15 at 11:46

1 Answers1

3

Using LinqToXML for example:

XDocument doc = XDocument.Parse(xml);

        var resultChannels =
            doc.Descendants("channel")
                .Select(
                        c =>
                            new epg2()
                            {
                                channelname = c.Attribute("name").Value,
                                EventNumber =
                                    c.Elements("event")
                                    .Select(e =>
                                    new Event()
                                    {
                                        name = e.Element("short_event_descriptor").Attribute("name").Value,
                                        starttime = DateTime.Parse(e.Attribute("start_time").Value),
                                        duration = int.Parse(e.Attribute("duration").Value),
                                        shortDescription = e.Element("short_event_descriptor").Value,
                                        longDescription = e.Element("extended_event_descriptor").Element("text").Value
                                    }).ToList()
                            }).ToList();
Florian Schmidinger
  • 4,682
  • 2
  • 16
  • 28