user1173169
user1173169

Reputation:

XDocument.Descendants() returns NullReferenceException : Object reference not set to an instance of an object

I have some difficulties to retrieve some datas in my XML file. This is what my XML looks like:

<?xml version="1.0" encoding="windows-1252"?>
<hexML version="0.9">
<head>
<title><![CDATA[Title ]]></title>
<description/>
<ftm date="2014-09-24T16:34:37 CET"/>
</head>
<body>
    <press_releases>
        <press_release id="1796257" language="fr" type="5">
            <published date="2014-06-19T11:55:09 CET"/>
            <categories>
            <category id="75" label="French" keywords="language"/>
            </categories>
            <headline><![CDATA[Test Release for Website 3]]></headline>
            <main><![CDATA[TEXT XML DETAILLE]]></main>
            <footer><![CDATA[]]></footer>
            <files>
                <file id="618383" format="pdf" type="Regular Attachment">
                <file_headline><![CDATA[Test Attachment]]></file_headline>
                <location><![CDATA[http://test.html1796257/618383.pdf]]></location>
                </file>
            </files>
            <location href="/S/151406/1796257.xml"/>
        </press_release>
    </press_releases>
</body>
</hexML>

i try to get this data : http://test.html1796257/618383.pdf (in the "files" tag)

This is what i tried so far :

            string Linkpdf = (from c in DetailXml.Descendants("files")                                  
                             select c.Element("location").Value).Single();

This returns me the exception mentionned above. Thanks for your help

Upvotes: 0

Views: 692

Answers (1)

har07
har07

Reputation: 89285

If the XML was indented properly :

<files>
    <file id="618383" format="pdf" type="Regular Attachment">
        <file_headline><![CDATA[Test Attachment]]></file_headline>
        <location><![CDATA[http://test.html1796257/618383.pdf]]></location>
    </file>
</files>

you'll be able to see clearly that <location> is direct child of <file> element within <files> :

string Linkpdf = (from c in DetailXml.Descendants("files")
                  select c.Element("file").Element("location").Value).Single();

Upvotes: 2

Related Questions