1

Not able to figure out what is the problem with following code and data. It returns "NullReferenceException was unhandled"

Code:

XmlDocument doc = new XmlDocument();
doc.Load(Filename1);

XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace("temp","http://www.Rahiman.com");

string name1 = doc.SelectSingleNode("//temp:Company/temp:Businesscard[2]/temp:Name", mgr).
    InnerText;

Console.WriteLine(name1);

Data File:

<?xml version="1.0" encoding="utf-8" ?>
<Company     xmlns="http://www.Rahiman.com">
  <![CDATA[This data file is created as part of 1st BizTalk example]]>
  <Businesscard>
    <name> Rahiman </name>
    <Phone Category="Mobile">+91 900028xxxx</Phone>
    <Phone Category="Land">+91 40-40020xxxx</Phone>
    <Phone Category="Fax">+91 900028xxxx</Phone>
    <Email>John@Yahoo.com</Email>
  </Businesscard>
  <Businesscard>
    <name>Shaik </name>
    <Phone Category="Mobile">+91 900028xxxx</Phone>
    <Phone Category="Land">+91 40-40020xxxx</Phone>
    <Phone Category="Fax">+91 900028xxxx</Phone>
    <Email>John@Yahoo.com</Email>
  </Businesscard>
</Company >

I have tried

string name1 = doc.DocumentElement.SelectSingleNode("//temp:Company/temp:Businesscard[2]/temp:Name", mgr).
    InnerText; 

as well

Thanks.

Gilad Green
  • 34,248
  • 6
  • 46
  • 76
Rahiman S
  • 31
  • 3
  • While I appreciate that technically all questions asking about NullRef exceptions could be marked as duplicates, the reasons why nullref may occur are many and varied and often it's that "need a second pair of eyes" aspect of the request, not necessarily the null ref itself, that is being sought. As such, directing to a generic advice (actually, like I've given in my answer) isn't going to be as helpful as eg Gilad has given as to why the null is occurring – Caius Jard Aug 02 '17 at 04:53
  • @CaiusJard StackOverflow isn't a debugging service, however. In fact, after reading the linked duplicate, it's highly likely OP will understand *what* the error means, and *how* to debug it. If it turns out the library is incorrectly returning null, when it shouldn't, then that's an entirely different question, and could be posted as such. – Rob Aug 03 '17 at 05:32
  • I partly accept the argument, but nearly every question on this site falls into one of two categories: "how do I do X" and "here's my broken solution to how to do X, please help fix it". In the case of the former, the questioner gets hammered for not putting any effort in, and maybe that's fair. In the case of the latter, we can't start hammering them with the "we do not exist to debug your code" stick; everyone needs a little help understanding the complexities of compiler and runtime error messages at some point in their early career and sometimes the cause is so subtle it's easy to miss. – Caius Jard Aug 03 '17 at 06:13

2 Answers2

2

When retrieving the Name tag it is case sensitive so it should be name

"//temp:Company/temp:Businesscard[2]/temp:name"

You can also use the ?. null propagation:

string name1 = doc.SelectSingleNode("//temp:Company/temp:Businesscard[2]/temp:name", mgr)?.InnerText;
Graham
  • 6,577
  • 17
  • 55
  • 76
Gilad Green
  • 34,248
  • 6
  • 46
  • 76
  • Note, of course, that we aren't certain he uses c# 6, but it's a handy tip if he does and hasn't come across the syntax before. For more info on it google "c# 6 null propagation". (I wasn't the downvorer; I think your advice is excellent here, so I've no problem cancelling out that dv with an uv. Let's see if there's any feedback from the dver) – Caius Jard Aug 02 '17 at 04:48
  • @CaiusJard - true but in any case the problem isn't the accessing of the inner text at all but the xpath used - the `name` vs `Name` – Gilad Green Aug 02 '17 at 04:50
  • `but indexes start from 0`... no, this is an xpath query, indices are 1-based. – Jeff Mercado Aug 02 '17 at 04:51
  • @JeffMercado - sorry :) corrected. Thanks for pointing it out – Gilad Green Aug 02 '17 at 04:54
  • The xml in the question also appears not well formed at the end node.. – Caius Jard Aug 02 '17 at 04:55
  • @CaiusJard - true but that is probably a typo of when posting because if it was that the exception was different (tried it :) ) – Gilad Green Aug 02 '17 at 04:57
  • Thanks for your help and time. It is very bad on my part I missed simple point case sensitive "Name" vs. "name". – Rahiman S Aug 02 '17 at 06:29
  • @RahimanS - That's ok :) If this helped you solve it please consider marking question as solved by the answer – Gilad Green Aug 02 '17 at 10:09
0

Well, it can't be that doc is null, so it must be hat the result of the SelectSingleNode method call is null. Don't refer to the .InnerText property until you're sure it isn't null:

string name1;
var n = doc.DocumentElement.SelectSingleNode("//temp:Company/temp:Businesscard[2]/temp:Name", mgr);
if(n!=null)
  name1 = n.InnerText;
Caius Jard
  • 47,616
  • 4
  • 34
  • 62