Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am really stuck on this one even though it seems like what I want is simple.
I have ObjXmlSimpleTypeDoc object which have two EnumerationValue nodes. Each of EnumerationValue have child nodes.

I want to find the correct EnumerationValue. To find the correct EnumerationValue node, I use a variable strCourtNCIC. If the value in strCourtNCIC (in this case MN010015J. This value can be some other value) matches a EnumerationValue@code, that is the EnumerationValue node I need.
For this question strCourtNCIC hold a value MN010015J.

How do I do this in VB.NET? My VB.NET code returns Nothing for objXmlEnumerationValueNode even though I am expecting to see the node with @code = MN010015J

Here is the objXmlSimpleTypeDoc object where I need to find EnumerationValue with @code = strCourtNCIC (MN010015J)
XML
<SimpleTypeCompanion enumerates="CourtLocationTextType">
	<EnumerationValue code="MN010015J">
		<Text>Emily County</Text>
		<AssociatedValue type="MNCISNodeID">
			<Text>111</Text>
		</AssociatedValue>
		<AssociatedValue type="CountyName">
			<Text>Emily</Text>
		</AssociatedValue>
		<AssociatedValue type="PhoneNumber">
			<Text>724-820-7123</Text>
		</AssociatedValue>
	</EnumerationValue>
	<EnumerationValue code="DC19DAKDC">
		<Text>Pope County</Text>
		<AssociatedValue type="MNCISNodeID">
			<Text>112</Text>
		</AssociatedValue>
		<AssociatedValue type="CountyName">
			<Text>Pope</Text>
		</AssociatedValue>
	</EnumerationValue>
</SimpleTypeCompanion>


What I have tried:

Here is the VB.NET code that I need help with to just get the correct EnumerationValue that matches strCourtNCIC (MN010015J).

VB
'CourtNCIC 
strCourtNCIC = objXmlMNCISData.DocumentElement.SelectSingleNode("Case/Court/CourtNCIC").InnerText
'Access the CourtLocationTextType simple type. 
objXmlSimpleTypeDoc = Msc.Integration.CourtXml.Library.v4.SimpleType.GetCompanionFile("CourtLocationTextType")

'Get the correct EnumerationValue node that has @code =MN010015J string value
objXmlEnumerationValueNode = objXmlSimpleTypeDoc.SelectSingleNode("/SimpleTypeCompanion/EnumerationValue[@code=" + strCourtORI + "]/@code")
Posted
Updated 13-Aug-19 10:39am
v2

Another way is to use XDocument class[^]:
VB.NET
Dim xcontent As String = "<SimpleTypeCompanion enumerates='CourtLocationTextType'>
	<EnumerationValue code='MN010015J'>
		<Text>Emily County</Text>
		<AssociatedValue type='MNCISNodeID'>
			<Text>111</Text>
		</AssociatedValue>
		<AssociatedValue type='CountyName'>
			<Text>Emily</Text>
		</AssociatedValue>
		<AssociatedValue type='PhoneNumber'>
			<Text>724-820-7123</Text>
		</AssociatedValue>
	</EnumerationValue>
	<EnumerationValue code='DC19DAKDC'>
		<Text>Pope County</Text>
		<AssociatedValue type='MNCISNodeID'>
			<Text>112</Text>
		</AssociatedValue>
		<AssociatedValue type='CountyName'>
			<Text>Pope</Text>
		</AssociatedValue>
	</EnumerationValue>
</SimpleTypeCompanion>"

Dim strCourtORI As String = "MN010015J"

Dim xdoc As XDocument = XDocument.Parse(xcontent)
Dim result = xdoc.Descendants() _
	.Where(Function(x) x.Name="EnumerationValue" AndAlso x.Attribute("code").Value = strCourtORI) _
	.FirstOrDefault()
'done! 
 
Share this answer
 
Comments
Richard Deeming 14-Aug-19 4:22am    
FYI, that could fail with a NullReferenceException if there was an EnumerationValue element without a code attribute.

The explicit cast operators which cast to a reference type or a nullable value type will not fail if the element or attribute is null, so it might be safer to use that.
.Where(Function(x) x.Name = "EnumerationValue" AndAlso CStr(x.Attribute("code")) = strCourtORI) _
Maciej Los 14-Aug-19 5:44am    
Good point, Richard. Thank you.
You haven't shown us where you initialize the strCourtORI variable. But looking at your data, the most likely problem is that you're missing quotes around the value:
VB.NET
objXmlEnumerationValueNode = objXmlSimpleTypeDoc.SelectSingleNode("/SimpleTypeCompanion/EnumerationValue[@code='" + strCourtORI + "']")
Also note that you were trying to select the attribute itself, rather than the EnumerationValue node.
 
Share this answer
 
Comments
Member 11403304 13-Aug-19 12:16pm    
Hi Richard, you have solved my issue. Yes I was missing the single quotes around the value. You have stopped all the pain I went through for the last many days. Something so simple yet so difficult to figure out!
Maciej Los 13-Aug-19 16:36pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900