3

I have been trying to understand how to work with xml-files using linq.

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<podcasts>
  <podcast>
    <url>http://...</url>
    <name>blablabla</name>
    <frequency>20 min</frequency>
    <category>Drama</category>
    <episodes>
      <episode>
        <name>blablabla</name>
        <description>blablabla...</description>
      </episode>
      <episode>
        <name>blablabla</name>
        <description>blablabla...</description>
      </episode>
    </episodes>
  </podcast>
  <podcast>
    <url>http://...</url>
    <name>blablabla</name>
    <frequency>20 min</frequency>
    <category>Drama</category>
    <episodes>
      <episode>
        <name>blablabla</name>
        <description>blablabla...</description>
      </episode>
      <episode>
        <name>blablabla</name>
        <description>blablabla...</description>
      </episode>
    </episodes>
  </podcast>
</podcasts>

The problem is that i can retrive the podcast-info, but not the episodes to that podcast. The podcastlist gets the podcast-info, but the episodelist in the podcast object dont get the episod-info. When i debug it says that episodelist is null..

And the Podcast and Episode class looks like this

public class Podcast
    {
        public string Url { get; set; }
        public string EpisodesQuantity { get; set; }
        public string Name { get; set; }
        public string Frequency { get; set; }
        public string Category { get; set; }
        public List<Episode> episodeList { get; set; }

    }    


public class Episode
    {
        public string Name { get; set; }
        public string Desc { get; set; }
        public string Link { get; set; }
    }

Im really stuck here, do u guys know what i'm doing wrong? :(

3
  • Why not just deserialize the xml? new XmlSerializer(typeof(Podcast[])).Deserialize(xmlReader); Commented Nov 1, 2018 at 14:47
  • 2
    @MrObama why did you remove the code from the question? Commented Nov 1, 2018 at 15:07
  • @MrObama I was trying to help you. People will probably start to downvote your question, if you remove the code. They will think you have made absolutely no effort in order to solve your problem. Commented Nov 1, 2018 at 15:19

2 Answers 2

2

This should be right. I have similiar thing, just edited to fit your need. I quess I you will get oriented in that.

episodeList = (from e in p.Element("episodes").Elements("episode")
                    select new Episode
                    {
                        Name = e.Element("name").Value,
                        Desc = e.Element("description").Value
                    }).ToList()

I have code loading very complex XML. So you can see the logic. Get a look.

 public static EdiFile read(XElement xmlDoc)
        {
            return new EdiFile
            {
                SPPLR_MAILBOX = xmlDoc.Element("SPPLR_MAILBOX").Value,
                MESSAGE_ID = xmlDoc.Element("MESSAGE_ID").Value,
                ASN_NO = xmlDoc.Element("ASN_NO").Value,
                MESSAGE_SEND_DATE = xmlDoc.Element("MESSAGE_SEND_DATE").Value,
                ETD = xmlDoc.Element("ETD").Value,
                ETA = xmlDoc.Element("ETA").Value,
                INVOICE_NUM = xmlDoc.Element("INVOICE_NUM").Value,
                SPPLR_CD = xmlDoc.Element("SPPLR_CD").Value,
                SPPLR_CONTACT = xmlDoc.Element("SPPLR_CONTACT").Value,
                DELIVERY_PARTY_CD = xmlDoc.Element("DELIVERY_PARTY_CD").Value,
                DELIVERY_TO_PLACE = xmlDoc.Element("DELIVERY_TO_PLACE").Value,
                DELIVERY_FROM_PLACE = xmlDoc.Element("DELIVERY_FROM_PLACE").Value,
                SHIPPING_TYPE = xmlDoc.Element("SHIPPING_TYPE").Value,
                FREIGHT_TERMS = xmlDoc.Element("FREIGHT_TERMS").Value,
                TRUCK_CONTACT = xmlDoc.Element("TRUCK_CONTACT").Value,
                TRUCK_LICENCE_NUM = xmlDoc.Element("TRUCK_LICENCE_NUM").Value,
                CREATED_BY = xmlDoc.Element("CREATED_BY").Value,
                CREATED_DATE = xmlDoc.Element("CREATED_DATE").Value,
                ATTRIBUTE01 = xmlDoc.Element("ATTRIBUTE01").Value,
                ATTRIBUTE02 = xmlDoc.Element("ATTRIBUTE02").Value,
                ATTRIBUTE03 = xmlDoc.Element("ATTRIBUTE03").Value,
                ATTRIBUTE04 = xmlDoc.Element("ATTRIBUTE04").Value,
                ATTRIBUTE05 = xmlDoc.Element("ATTRIBUTE05").Value,
                Levels0 = (from a in xmlDoc.Element("LEVELS0").Elements("LEVEL0")
                           select new Level0
                           {
                               PLT_NUM = a.Element("PLT_NUM").Value,
                               PLT_LABEL_ID = a.Element("PLT_LABEL_ID").Value,
                               BOX_COUNT = a.Element("BOX_QTY").Value,
                               PLT_WEIGHT = a.Element("PLT_WEIGTH").Value,
                               PLT_DIMENSION = a.Element("PLT_DIMENSION").Value,
                               PLT_CHEM = a.Element("PLT_CHEM").Value,
                               PLT_NOT_STACK = a.Element("PLT_NOT_STACK").Value,
                               PLT_NOTE = a.Element("PLT_NOTE").Value,
                               ATTRIBUTE01 = a.Element("ATTRIBUTE01").Value,
                               ATTRIBUTE02 = a.Element("ATTRIBUTE02").Value,
                               ATTRIBUTE03 = a.Element("ATTRIBUTE03").Value,
                               ATTRIBUTE04 = a.Element("ATTRIBUTE04").Value,
                               ATTRIBUTE05 = a.Element("ATTRIBUTE05").Value,
                               Levels1 = (from b in a.Element("LEVELS1").Elements("LEVEL1")
                                          select new Level1
                                          {
                                              BOX_NUM = b.Element("BOX_NUM").Value,
                                              BOX_LABEL_ID = b.Element("BOX_LABEL_ID").Value,
                                              Items = (from c in b.Element("ITEMS").Elements("ITEM")
                                                       select new Item
                                                       {
                                                           SPPLR_ITEM = c.Element("SPPLR_ITEM").Value,
                                                           CUST_ITEM = c.Element("CUST_ITEM").Value,
                                                           PO_NO = c.Element("PO_NO").Value,
                                                           PO_REF = c.Element("PO_REF").Value,
                                                           COUNTRY_OF_ORIGIN = c.Element("COUNTRY_OF_ORIGIN").Value,
                                                           BATCH_NO = c.Element("BATCH_NO").Value,
                                                           MLS_CLASS = c.Element("MLS_CLASS").Value,
                                                           ATTRIBUTE01 = c.Element("ATTRIBUTE01").Value,
                                                           ATTRIBUTE02 = c.Element("ATTRIBUTE02").Value,
                                                           ATTRIBUTE03 = c.Element("ATTRIBUTE03").Value,
                                                           ATTRIBUTE04 = c.Element("ATTRIBUTE04").Value,
                                                           ATTRIBUTE05 = c.Element("ATTRIBUTE05").Value,
                                                           Lots = (from d in c.Element("LOTS").Elements("LOT")
                                                                   select new Lot
                                                                   {
                                                                       LOT_NUM = d.Element("LOT_NUM").Value,
                                                                       LOT_LABEL_ID = d.Element("LOT_LABEL_ID").Value,
                                                                       LOT_NOTE = d.Element("LOT_NOTE").Value,
                                                                       LOT_EXP_DATE = d.Element("LOT_EXP_DATE").Value,
                                                                       QTY = d.Element("QTY").Value,
                                                                       UOM = d.Element("UOM").Value,
                                                                       ATTRIBUTE01 = d.Element("ATTRIBUTE01").Value,
                                                                       ATTRIBUTE02 = d.Element("ATTRIBUTE02").Value,
                                                                       ATTRIBUTE03 = d.Element("ATTRIBUTE03").Value,
                                                                       ATTRIBUTE04 = d.Element("ATTRIBUTE04").Value,
                                                                       ATTRIBUTE05 = d.Element("ATTRIBUTE05").Value
                                                                   }).ToList()
                                                       }).ToList()
                                          }).ToList()
                           }).ToList()
            };
        }

Thats just if you were curios, you can see clearly how it works in my example here.

Sign up to request clarification or add additional context in comments.

4 Comments

Why do you need from..in here? It won't work because no element with name desription
Your code will probably thrown a NullReferenceException - it should be Desc = e.Element("description").Value, not Desc = e.Element("desription").Value.
@RuiJarimba ye, sorry I see that, I didnt notice the description typo, I just copied his part and edited it, my bad.
@RomanMarusyk And the from...in, I like using that because of code clearance. You just can get oriented in the structure better (for me)
0

use this

episodeList = p.Element("episodes").Elements("episode")
    .Select(e => new Episode
    {
        Name = e.Element("name").Value,
        Desc = e.Element("description").Value
    }).ToList()

and note that you have a typo here e.Element("desription") (sould be "description")

Comments

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.