user1360664
user1360664

Reputation:

Convert XML to a list of something

I have a xml file coming from my php site as an api.

This is the xml that is coming back from my php application.

<xml>
<overzicht>
<item>
<sessieID>6</sessieID>
<onderwerp>Vrijwilligers, een uitstervend rasnn</onderwerp>
<omschrijving>Ode aan de vrijwilligers jjj</omschrijving>
<sprekerID>1</sprekerID>
<lokaalID>20</lokaalID>
<themaID>1</themaID>
<typeID>2</typeID>
<periodeID>2</periodeID>
<datum>2012-02-20</datum>
<maximaleInschrijvingen>1</maximaleInschrijvingen>
<spreker>
    <sprekerID>1</sprekerID>
    <sprekerNaam>Rik Torfs</sprekerNaam>
    <loginID>13</loginID>
</spreker>
<lokaal>
    <lokaalID>20</lokaalID>
    <campusNaam>Malle</campusNaam>
    <lokaalOpCampus>W10</lokaalOpCampus>
    <typeID>2</typeID>
</lokaal>
</item>
<item>
<sessieID>15</sessieID>
<onderwerp>VPKB</onderwerp>
<omschrijving/>
<sprekerID>6</sprekerID>
<lokaalID>2</lokaalID>
<themaID>1</themaID>
<typeID>1</typeID>
<periodeID>2</periodeID>
<datum>2012-02-20</datum>
<maximaleInschrijvingen>50</maximaleInschrijvingen>
<spreker>
    <sprekerID>6</sprekerID>
    <sprekerNaam>Dick Wursten</sprekerNaam>
    <loginID>18</loginID>
</spreker>
<lokaal>
    <lokaalID>2</lokaalID>
    <campusNaam>KHK Vorselaar</campusNaam>
    <lokaalOpCampus>A102</lokaalOpCampus>
    <typeID>1</typeID>
</lokaal>
</item>
...
</overzicht>
</xml>

This is my C# code. I want to get al list of Sessie.

XDocument xmlDoc = XDocument.Parse(e.Result);
List<Sessie> sessies = 
    (
        from item in xmlDoc.Descendants("overzicht")
        select new Sessie(
                item.Element("onderwerp").Value,
                Convert.ToInt32(item.Element("sessieID").Value),
                item.Element("omschrijving").Value,
                (Spreker)(
                            new Spreker(
                                Convert.ToInt32(item.Element("spreker").Element("sprekerID").Value), 
                                item.Element("spreker").Element("sprekernaam").Value)
                        ),
                Convert.ToDateTime(item.Element("datum").Value),
                Convert.ToInt32(item.Element("maximaleInschrijvingen").Value),
                (Lokaal)(
                            new Lokaal(
                                Convert.ToInt32(item.Element("lokaal").Element("lokaalID").Value), 
                                item.Element("lokaal").Element("campusNaam").Value, 
                                item.Element("lokaal").Element("lokaalOpCampus").Value)
                        )
                )
    ).ToList<Sessie>();

I know my code isn't working with this exception.

"NullReferenceException"

Upvotes: 1

Views: 380

Answers (2)

Henk Holterman
Henk Holterman

Reputation: 273179

The query

 from item in xmlDoc.Descendants("overzicht")

will return a list of <overzicht> elements. item.Element("onderwerp") does not exist, you are missing the <item> element in between.

Simple fix:

 from item in xmlDoc.Descendants("item")

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1499800

There's one fairly obvious problem to start with. Look at the very start of your query:

from item in xmlDoc.Descendants("overzicht")
select new Sessie(item.Element("onderwerp").Value,
...

That will only work if there's an <onderwerp> directly under <overzicht>. There isn't - it's under the <item> element. Perhaps (given the range variable name) you meant:

from item in xmlDoc.Descendants("item")
select new Sessie(item.Element("onderwerp").Value,
...

Upvotes: 3

Related Questions