Click here to Skip to main content
14,265,784 members
Rate this:
Please Sign up or sign in to vote.
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)
<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).

'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 4 days ago
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

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:
objXmlEnumerationValueNode = objXmlSimpleTypeDoc.SelectSingleNode("/SimpleTypeCompanion/EnumerationValue[@code='" + strCourtORI + "']")
Also note that you were trying to select the attribute itself, rather than the EnumerationValue node.
   
Comments
Member 11403304 4 days ago
   
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 4 days ago
   
5ed!
Rate this:
Please Sign up or sign in to vote.

Solution 2

Another way is to use XDocument class[^]:
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! 
   
Comments
Richard Deeming 3 days ago
   
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 3 days ago
   
Good point, Richard. Thank you.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100