Solved

Ignore missing nodes in XMLDocument

Posted on 2010-11-30
7
373 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now