c#reading node给出错误:对象引用未设置为对象的实例

时间:2016-04-14 11:43:48

标签: c# xml ibm-connections

当我尝试从以下xml文件中读取节点(条目 - >贡献者 - >名称)时,我会收到错误,您可以在此处看到:error message

<?xml version="1.0" encoding="UTF-8"?>
<entry
    xmlns:app="http://www.w3.org/2007/app"
    xmlns:thr="http://purl.org/syndication/thread/1.0"
    xmlns:fh="http://purl.org/syndication/history/1.0"
    xmlns:snx="http://www.ibm.com/xmlns/prod/sn"
    xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"
    xmlns="http://www.w3.org/2005/Atom">
    <id>tag:profiles.ibm.com,2006:entry2f377e12-dfdc-4634-9d32-36dcdc685a5a</id>
    <title type="text">Behnke Steffen (DS/MFT)</title>
    <updated>2016-04-07T12:46:23.328Z</updated>
    <category term="profile" scheme="http://www.ibm.com/xmlns/prod/sn/type"></category>
    <contributor>
        <name>Behnke Steffen (DS/MFT)</name>
        <snx:userid>901F7D0D-E2F7-4094-8864-8F4C67716685</snx:userid>
        <email>Steffen.Behnke@de.bosch.com</email>
        <snx:userState>active</snx:userState>
        <snx:isExternal>false</snx:isExternal>
    </contributor>
    <link href="https://connect.bosch.com/profiles/atom/profileEntry.do?key=2f377e12-dfdc-4634-9d32-36dcdc685a5a" rel="self" type="application/atom+xml"></link>
    <link href="https://connect.bosch.com/profiles/atom/profileType.do?type=default" rel="http://www.ibm.com/xmlns/prod/sn/profile-type" type="application/profile-type+xml"></link>
    <link href="https://connect.bosch.com/profiles/html/profileView.do?key=2f377e12-dfdc-4634-9d32-36dcdc685a5a" rel="related" type="text/html"></link>
    <link href="https://connect.bosch.com/profiles/photo.do?key=2f377e12-dfdc-4634-9d32-36dcdc685a5a&amp;lastMod=1460033183328" rel="http://www.ibm.com/xmlns/prod/sn/image" type="image"></link>
    <link href="https://connect.bosch.com/profiles/audio.do?key=2f377e12-dfdc-4634-9d32-36dcdc685a5a&amp;lastMod=1460033183328" rel="http://www.ibm.com/xmlns/prod/sn/pronunciation" type="audio"></link>
    <link href="https://connect.bosch.com/profiles/vcard/profile.do?key=2f377e12-dfdc-4634-9d32-36dcdc685a5a" rel="alternate" type="text/directory"></link>
    <summary type="text">Profile information for Behnke Steffen (DS/MFT)</summary>
    <content type="xhtml">
        <sp_0:div
            xmlns="http://www.w3.org/1999/xhtml"
            xmlns:sp_0="http://www.w3.org/1999/xhtml">
            <span class="vcard">
                <div>
                    <img src="https://connect.bosch.com/profiles/photo.do?key=2f377e12-dfdc-4634-9d32-36dcdc685a5a&amp;lastMod=1460033183328" class="photo"></img>
                </div>
                <div>
                    <a class="fn url" href="https://connect.bosch.com/profiles/atom/profile.do?key=2f377e12-dfdc-4634-9d32-36dcdc685a5a">Behnke Steffen (DS/MFT)</a>
                </div>
                <div>
                    <a class="sound url" href="https://connect.bosch.com/profiles/audio.do?key=2f377e12-dfdc-4634-9d32-36dcdc685a5a&amp;lastMod=1460033183328">Pronunciation</a>
                </div>
                <div>
                    <a class="email" href="mailto:Steffen.Behnke@de.bosch.com">Steffen.Behnke@de.bosch.com</a>
                </div>
                <div class="x-groupwareMail" style="display:none"></div>
                <div class="org">
                    <span class="organization-unit"></span>
                </div>
                <div class="role"></div>
                <div class="title">Manufacturing Technology Management</div>
                <div class="adr work postal">
                    <div class="x-country-code" style="display:none">de</div>
                </div>
                <div class="x-office">
                    <span class="x-building">Fe 665</span>
                    <span class="x-floor">2</span>
                    <span class="x-office-number">Fe 665/2</span>
                </div>
                <div class="tel">
                    <abbr class="type" title="work">Work:</abbr>
                    <span class="value">+49(711)811-30379</span>
                </div>
                <div class="x-manager-uid" style="display:none"></div>
                <div class="x-is-manager" style="display:none"></div>
                <div class="x-profile-key">2f377e12-dfdc-4634-9d32-36dcdc685a5a</div>
                <div class="uid">901F7D0D-E2F7-4094-8864-8F4C67716685</div>
                <div class="x-profile-uid">bhs2si</div>
                <div class="x-lconn-userid">901F7D0D-E2F7-4094-8864-8F4C67716685</div>
                <div class="rev" style="display:none">2016-04-07T12:46:23.328Z</div>
                <div class="x-profile-type" style="display:none">default</div>
            </span>
        </sp_0:div>
    </content>
</entry>

这是我的c#代码

XmlDocument doc = new XmlDocument();
doc.LoadXml(File.ReadAllText(pathToXmlFile));

XmlNode node = doc.DocumentElement.SelectSingleNode("/entry/contributor/name");

Console.WriteLine(node.InnerText);

我100%确定我的c#代码是正确的,因为当我尝试从其他更小/更简单的xml文件中读取节点时,我没有得到错误并得到我想要的结果,因此xml文件可能有问题。

谢谢你的帮助:)

0 个答案:

没有答案