?
Solved

Ignore missing nodes in XMLDocument

Posted on 2010-11-30
7
Medium Priority
?
379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

765 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