Answered by:
How do I check if section and subdivision exist inside object's xml?

Question
-
My function is working but not as I expect it to. The problem is that when Section and Subvision are not found in the object aobjXmlGetStatuteRequestNode xml, I get an error NullReference Exception was unhandled by user code. Object reference not set to an instance of an object.
For that reason, because the two elements are optional, I would like to check if they exist before adding their value into the xml inside the object objXmlRequestMessageDoc.
The if statement I used in my function did not work. When it found Section element, it did not check for Subdivision.
How do I check for both and add them if they exist. If they do not exist, I do not need to do anything and should not throw an error since they are optional.
Object aobjXmlGetStatuteRequestNode has this xml
<ns:GetStatutesRequest xmlns:ns="http://www.courts.state.mn.us/StatuteService/1.0"> <ns:Statute> <ns:Chapter>169</ns:Chapter> <!--Optional:--> <ns:Section>191</ns:Section> <!--Optional:--> <ns:Subdivision>a</ns:Subdivision> </ns:Statute>
The following output in the objXmlRequestMessageDoc object is correct but this does not work when Section and or Subdivision are not found in the object aobjXmlGetStatuteRequestNode.
<ns:BasicSearchQueryRequest xmlns:ns="http://crimnet.state.mn.us/mnjustice/statute/service/4.0"> <ns1:BasicSearchCriteria xmlns:ns1="http://crimnet.state.mn.us/mnjustice/statute/messages/4.0"> <ns2:Chapter xmlns:ns2="http://crimnet.state.mn.us/mnjustice/statute/4.0"/169</ns2:Chapter>> <ns2:Section xmlns:ns2="http://crimnet.state.mn.us/mnjustice/statute/4.0"/>191</ns2:Chapter> <ns2:Subdivision xmlns:ns2="http://crimnet.state.mn.us/mnjustice/statute/4.0"/>a</ns2:Chapter> </ns1:BasicSearchCriteria> </ns:BasicSearchQueryRequest>
My Function
Function GetStatutesByChapter(ByVal aobjXmlGetStatuteRequestNode As XmlNode, ByVal aobjXMLNameSpaceManager As XmlNamespaceManager, ByVal aobjBroker As ServiceCatalog.Library.v4.Broker) As XmlDocument Dim objXmlRequestMessageDoc As XmlDocument Dim objXmlResponseMessageDoc As XmlDocument Dim intCount As Integer aobjBroker.PostMessageWarehouseInformationalMessage("Chapter found.", 1) objXmlResponseMessageDoc = New XmlDocument 'Add the first element into the document GetStatuteByChapter with its namespace objXmlResponseMessageDoc.AppendChild(objXmlResponseMessageDoc.CreateElement("BasicSearchQueryResponse", "http://crimnet.state.mn.us/mnjustice/statute/service/4.0")) 'Create the BCA request message objXmlRequestMessageDoc = New XmlDocument objXmlRequestMessageDoc.AppendChild(objXmlRequestMessageDoc.CreateElement("ns:BasicSearchQueryRequest", aobjXMLNameSpaceManager.LookupNamespace("ns"))) objXmlRequestMessageDoc.SelectSingleNode("ns:BasicSearchQueryRequest", aobjXMLNameSpaceManager).AppendChild(objXmlRequestMessageDoc.CreateElement("ns1:BasicSearchCriteria", aobjXMLNameSpaceManager.LookupNamespace("ns1"))) objXmlRequestMessageDoc.SelectSingleNode("ns:BasicSearchQueryRequest/ns1:BasicSearchCriteria", aobjXMLNameSpaceManager).AppendChild(objXmlRequestMessageDoc.CreateElement("ns2:Chapter", aobjXMLNameSpaceManager.LookupNamespace("st"))) objXmlRequestMessageDoc.SelectSingleNode("ns:BasicSearchQueryRequest/ns1:BasicSearchCriteria", aobjXMLNameSpaceManager).AppendChild(objXmlRequestMessageDoc.CreateElement("ns2:Section", aobjXMLNameSpaceManager.LookupNamespace("st"))) objXmlRequestMessageDoc.SelectSingleNode("ns:BasicSearchQueryRequest/ns1:BasicSearchCriteria", aobjXMLNameSpaceManager).AppendChild(objXmlRequestMessageDoc.CreateElement("ns2:Subdivision", aobjXMLNameSpaceManager.LookupNamespace("st"))) objXmlRequestMessageDoc.DocumentElement.SelectSingleNode("ns1:BasicSearchCriteria/st:Chapter", aobjXMLNameSpaceManager).InnerText = aobjXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:Chapter", aobjXMLNameSpaceManager).InnerText objXmlRequestMessageDoc.DocumentElement.SelectSingleNode("ns1:BasicSearchCriteria/st:Section", aobjXMLNameSpaceManager).InnerText = aobjXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:Section", aobjXMLNameSpaceManager).InnerText objXmlRequestMessageDoc.DocumentElement.SelectSingleNode("ns1:BasicSearchCriteria/st:Subdivision", aobjXMLNameSpaceManager).InnerText = aobjXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:Subdivision", aobjXMLNameSpaceManager).InnerText 'check if there is a section and or subdivision if it is there then set the value 'If Not (aobjXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:Section", aobjXMLNameSpaceManager).InnerText) Is Nothing Then ' objXmlRequestMessageDoc.DocumentElement.SelectSingleNode("ns1:BasicSearchCriteria/st:Section", aobjXMLNameSpaceManager).InnerText = aobjXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:Section", aobjXMLNameSpaceManager).InnerText 'End If 'check if there is a section and or subdivision if it is there then set the value aobjBroker.PostMessageWarehouseSnapshot(objXmlRequestMessageDoc.OuterXml, "Request Message", 1) 'Call the BCA service intCount = 0 'Count how many Statute node found CType(objXmlResponseMessageDoc.SelectSingleNode("ss:GetStatutesResponse/ss:StatutesXml/ss:Statutes", aobjXMLNameSpaceManager), System.Xml.XmlElement).SetAttribute("totalCount", CStr(intCount)) Return objXmlResponseMessageDoc End Function
- Edited by winkimjr2 Monday, March 16, 2015 2:52 PM Explained the code for aobjXmlGetStatuteRequestNode
Answers
-
I have resolved this issue by using two if statements as follows
'check if there is a section and or subdivision if it is there then set the value If Not (aobjXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:Section", aobjXMLNameSpaceManager) Is Nothing) Then objXmlRequestMessageDoc.DocumentElement.SelectSingleNode("ns1:BasicSearchCriteria/st:Section", aobjXMLNameSpaceManager).InnerText = aobjXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:Section", aobjXMLNameSpaceManager).InnerText End If If Not (aobjXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:Subdivision", aobjXMLNameSpaceManager) Is Nothing) Then objXmlRequestMessageDoc.DocumentElement.SelectSingleNode("ns1:BasicSearchCriteria/st:Subdivision", aobjXMLNameSpaceManager).InnerText = aobjXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:Subdivision", aobjXMLNameSpaceManager).InnerText End If
- Proposed as answer by Rbie Monday, March 16, 2015 3:51 PM
- Marked as answer by Youjun TangModerator Tuesday, March 24, 2015 5:53 AM
All replies
-
I have resolved this issue by using two if statements as follows
'check if there is a section and or subdivision if it is there then set the value If Not (aobjXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:Section", aobjXMLNameSpaceManager) Is Nothing) Then objXmlRequestMessageDoc.DocumentElement.SelectSingleNode("ns1:BasicSearchCriteria/st:Section", aobjXMLNameSpaceManager).InnerText = aobjXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:Section", aobjXMLNameSpaceManager).InnerText End If If Not (aobjXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:Subdivision", aobjXMLNameSpaceManager) Is Nothing) Then objXmlRequestMessageDoc.DocumentElement.SelectSingleNode("ns1:BasicSearchCriteria/st:Subdivision", aobjXMLNameSpaceManager).InnerText = aobjXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:Subdivision", aobjXMLNameSpaceManager).InnerText End If
- Proposed as answer by Rbie Monday, March 16, 2015 3:51 PM
- Marked as answer by Youjun TangModerator Tuesday, March 24, 2015 5:53 AM
-
Hi winkimjr2,
So glad that you have solved your issue, and thanks for your sharing.
You could mark your own reply as answer to end this post.
Have a nice day.
Youjun Tang
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.