Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Ignore missing nodes in XMLDocument

Posted on 2010-11-30
7
Medium Priority
?
381 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
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

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!

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

609 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