[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Ignore missing nodes in XMLDocument

Posted on 2010-11-30
7
Medium Priority
?
383 Views
Last Modified: 2012-05-10
Hi Experts,

If I'm reading in using XMLReader like so:

            userSurName = m_node.SelectSingleNode("BillingData/BuyerName").InnerText
            userEmail = m_node.SelectSingleNode("BillingData/BuyerEmailAddress").InnerText
            userAddress1 = m_node.SelectSingleNode("FulfillmentData/Address/AddressFieldOne").InnerText
            userAddress2 = m_node.SelectSingleNode("FulfillmentData/Address/AddressFieldTwo").InnerText

...and sometimes one of these nodes doesn't exist, it will error with: "System.NullReferenceException: Object reference not set to an instance of an object."

what's the best way to deal with this, please?  

Thanks
0
Comment
Question by:jammy-d0dger
  • 4
  • 2
7 Comments
 
LVL 32

Assisted Solution

by:Erick37
Erick37 earned 800 total points
ID: 34239041
Test the node first before calling InnerText()

node = m_node.SelectSingleNode("BillingData/BuyerName")
If node IsNot Nothing Then
    userSurName = node.InnerText
End If
0
 
LVL 12

Assisted Solution

by:HugoHiasl
HugoHiasl earned 1200 total points
ID: 34239082
The problem is that the term

m_node.SelectSingleNode("FulfillmentData/Address/AddressFieldOne")

is not returning an object but you try directly to access the InnerText-Property of the object that was not returned.

You can check this for every row like:

userSurName = m_node.SelectSingleNode("BillingData/BuyerName") == null ? "" : m_node.SelectSingleNode("BillingData/BuyerName").InnerText:


Or you create a small function handling this more convenient:

userSurName = getMyNode(m_node, "BillingData/BuyerName");

private string getMyNode(XmlNode myNode, string xPathString) {
  return myNode.SelectSingleNode(xPathString) == null ? "" : myNode.SelectSingleNode(xPathString).InnerText:
}

0
 

Author Comment

by:jammy-d0dger
ID: 34239141
HugoHiasl: is there any way you can convert that to vb for me?  
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jammy-d0dger
ID: 34239359
OK, think I'm nearly there.  My conversion of what you suggested is:

    Public Shared Function getXMLNodeValue(ByVal currentNode As XmlNode, ByVal xPathString As String) As String
        If currentNode Is Nothing Then
            getXMLNodeValue = ""
        Else
            getXMLNodeValue = currentNode.SelectSingleNode(xPathString).InnerText
        End If
    End Function

called by:            
 userAddress2 = getXMLNodeValue(m_node, "FulfillmentData/Address/AddressFieldTwo")

However, when I run this, it all works except when AdressFieldTwo isn't present and then it still tries to do "getXMLNodeValue = currentNode.SelectSingleNode(xPathString).InnerText" which leads me to believe that "'Is Nothing" is not the correct test?


0
 

Accepted Solution

by:
jammy-d0dger earned 0 total points
ID: 34239453
Sorted it.  For completeness in case anyone else searches for this:

    Public Shared Function getXMLNodeValue(ByVal currentNode As XmlNode, ByVal xPathString As String) As String
        If currentNode.SelectSingleNode(xPathString) Is Nothing Then
            getXMLNodeValue = ""
        Else
            getXMLNodeValue = currentNode.SelectSingleNode(xPathString).InnerText
        End If
    End Function

I was comparing against the wrong node/path.  All good now.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 34239485
Use the method I gave you.
You should not call InnerText on the SelectSingleNode until you know that it is not Nothing.

Example:
Dim node As XMLNode = m_node.SelectSingleNode("BillingData/BuyerName")
If node IsNot Nothing Then
    userSurName = node.InnerText
Else
    userSurName = String.Empty
End If
0
 

Author Closing Comment

by:jammy-d0dger
ID: 34276707
Guys, thanks for the steer in the right direction. Few more points to Hugo, for suggesting the function approach which is much cleaner.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Loops Section Overview
Screencast - Getting to Know the Pipeline
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question