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

    }
1

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

9 Comments

i dont want the results to be array elements
you can iterate over this array(foreach) and get it one by one or you just do foreach(var item in doc.Descendants("event")) and get the values like in the annonymous type ininitializer
Its just one possiblility how to be able to do it.
Added example with foreach
No clue what the number in epg2 should be mapped to though
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.