Solved

Ignore missing nodes in XMLDocument

Posted on 2010-11-30
7
375 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 200 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 300 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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