0

when i try to read a node(entry -> contributor -> name) from following xml file i get an error which you can see here: 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>

here is my c# code

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

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

Console.WriteLine(node.InnerText);

i am 100% sure that my c# code is correct because when i try to read nodes from other and smaller/simpler xml files i get no error and get the result i want so there is probably something wrong with the xml file.

thank you for help :)

briben
  • 21
  • @Jeroen I dont think this is a duplicate. Rather it is problem with the xml namespaces. Should not be closed – Nitin Apr 14 '16 at 12:10
  • Whenever you find yourself thinking you're 100% sure, that's a good sign you're probably wrong. Your XML is perfectly valid, but your C# code isn't reading it. The elements live in the `http://www.w3.org/2005/Atom` namespace and should be selected as such. – Jeroen Mostert Apr 14 '16 at 12:11
  • @Nitin possibly, but until there is any debugging done from OP's side I'm leaving it closed. SO should not be used as a code + error dump. – Jeroen Vannevel Apr 14 '16 at 12:14
  • ok so how should my c# code look like when using the namespace? – briben Apr 14 '16 at 12:31
  • @briben you need the xmlns name space in your xpath /a:entry/a:contributor/a:name – Paul Bastide Apr 14 '16 at 13:03
  • @JeroenVannevel - Better to say it's a duplicate of [XmlDocument.SelectSingleNode and xmlNamespace issue](https://stackoverflow.com/questions/4171451/xmldocument-selectsinglenode-and-xmlnamespace-issue) since that is the specific problem here. – dbc Apr 14 '16 at 21:18
  • thanks to all for your help, it works fine now :) – briben Apr 15 '16 at 06:02

0 Answers0