简体   繁体   English

XDocument.Descendants()返回NullReferenceException:未将对象引用设置为对象的实例

[英]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. 我在检索XML文件中的某些数据时遇到一些困难。 This is what my XML looks like: 这是我的XML的样子:

<?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) 我尝试获取此数据: http://test.html1796257/618383.pdf (在“文件”标签中)

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 谢谢你的帮助

If the XML was indented properly : 如果XML缩进正确:

<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> : 您将清楚地看到<location><files><file>元素的直接子元素:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未将对象引用设置为具有XDocument的对象的实例 - Object reference not set to an instance of an object with XDocument XDocument.Descendants没有返回后代 - XDocument.Descendants not returning descendants 对象引用未设置为对象xdocument的实例 - object reference not set to an instance of an object xdocument NullreferenceException - 对象引用未设置为对象的实例 - NullreferenceException - Object Reference is not set to an instance of a object 未处理NullReferenceException,未将对象引用设置为对象的实例 - NullReferenceException was unhandled, Object Reference not set to an instance of an object NullReferenceException:对象引用未设置为对象#2的实例 - NullReferenceException: Object reference not set to an instance of an object #2 错误:NullReferenceException:未将对象引用设置为对象的实例 - ERROR: NullReferenceException: Object reference not set to an instance of an object XDocument.Descendants()与DescendantNodes() - XDocument.Descendants() versus DescendantNodes() 数据存储库实例Null-NullReferenceException吗? 你调用的对象是空的 - Data Repository instance Null - NullReferenceException? Object reference not set to an instance of an object XDocument.Descendants()过滤太多 - XDocument.Descendants() filters too much
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM