3 Replies - 267 Views - Last Post: 17 December 2018 - 07:05 AM Rate Topic: -----

#1 winkimjr2   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 30-May 12

How do I check if two Status elements are the same?

Posted 14 December 2018 - 10:59 AM

I would like to check if the first status in ProtectionOrder with Op="A" or "E" is the same as first status in MNProtectionOrderAdditional with Op="A" or "E".

If they are not equal, I will throw a system error like this
Throw New System.Exception("Statuses out of sync.  The detail tab does Not match the additional tab status.")


How do I do this using if statement in VB.Net?

In my Vb.Net code, the xml document is in aobjXmlInputDoc object. So to get Status element I will do it this way

In plain English my logic will look like this
If the first status with Op=A or E from ProtectionOrder <> to first Status from MNProtectionOrderAdditional Then
Throw New System.Exception("Statuses out of sync.  The detail tab does Not match the additional tab status.")
End If


In VB.Net I am not sure how to do it. This is where I need help.

If aobjXmlInputDoc.DocumentElement.SelectSingleNode("Integration/ProtectionOrder/Statuses/Status[1]/@Op") <> aobjXmlInputDoc.DocumentElement.SelectSingleNode ("Integration/ProtectionOrder/MNProtectionOrderAdditional/Statuses/Status[1]/@Op") Then
 Throw New System.Exception("Statuses out of sync.  The detail tab does Not match the additional tab status.")
End If



Here is my xml document.
<Integration>
	<ProtectionOrder>

		<Statuses>
			<Status Op="A">
				<Current>true</Current>
				<Active>No</Active>
				<Date Op="A">12/13/2018</Date>
				<Type Op="A" Word="EXPIRED">Expired</Type> 
			</Status>
			<Status>
				<Current>false</Current>
				<Active>Yes</Active>
				<Date>12/13/2016</Date>
				<Type Word="SBJO">Signed By Judicial Officer</Type>
			</Status>
			<Status>
				<Current>false</Current>
				<Active>No</Active>
				<Date>12/13/2016</Date>
				<Type Word="DRAFT">Draft</Type>
			</Status>
		</Statuses>

		<MNProtectionOrderAdditional>
			<Statuses>
				<Status Op="A">
					<Current>false</Current>
					<Active>No</Active>
					<Date Op="A">12/13/2018</Date>
					<Type Op="A" Word="EXPIRED">Expired</Type>
				</Status>
				<Status>
					<Current>false</Current>
					<Active>Yes</Active>
					<Date>12/13/2016</Date>
					<Type Word="SBJO">Signed By Judicial Officer</Type>
				</Status>
				<Status>
					<Current>true</Current>
					<Active>No</Active>
					<Date>12/13/2016</Date>
					<Type Word="DRAFT">Draft</Type>
				</Status>
			</Statuses>


		</MNProtectionOrderAdditional>
	</ProtectionOrder>
</Integration>


Is This A Good Question/Topic? 0
  • +

Replies To: How do I check if two Status elements are the same?

#2 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 195
  • View blog
  • Posts: 1,089
  • Joined: 05-December 13

Re: How do I check if two Status elements are the same?

Posted 14 December 2018 - 11:59 AM

Use an XML Reader to read the nodes, then use conditional operators to check if it is equal or not.

https://docs.microso...framework-4.7.2
Was This Post Helpful? 0
  • +
  • -

#3 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1532
  • View blog
  • Posts: 3,859
  • Joined: 01-February 13

Re: How do I check if two Status elements are the same?

Posted 15 December 2018 - 04:47 PM

I would suggest getting the 'Op' attributes into two single XmlNodes, then compare their Value properties. The Value property of an XmlNode is a String type and can be compared just like any other simple strings. This would make your life easier when writing the 'If Then' statement too.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim aobjXmlInputDoc As New XmlDocument
        aobjXmlInputDoc.Load("C:\TestFolder\TheXmlFile.xml")

        'get the two 'Op' attribute nodes
        Dim Op1 As XmlNode = aobjXmlInputDoc.DocumentElement.SelectSingleNode("Integration/ProtectionOrder/Statuses/Status[1]/@Op")
        Dim Op2 As XmlNode = aobjXmlInputDoc.DocumentElement.SelectSingleNode("Integration/ProtectionOrder/MNProtectionOrderAdditional/Statuses/Status[1]/@Op")

        'here i check if the value of the first 'Op' attribute is either "A" or "B" and also that the second 'Op' attribute is the same as the first one.
        If (Op1.Value = "A" OrElse Op1.Value = "E") AndAlso Op2.Value = Op1.Value Then
            MessageBox.Show("Status: " & Op1.Value & Environment.NewLine & "Additional Status: " & Op2.Value, "They Match Requirements...")
        End If
    End Sub



Being that your posted xml document is not complete, I can not tell if your real document has a root node that contains the <Integration> node but, that is how your code is set up to get the attribute values. So, it's not clear if that might be part of the problem you are having, or if you did not understand using the Value property to compare the node values.
Was This Post Helpful? 0
  • +
  • -

#4 dbasnett   User is offline

  • D.I.C Addict
  • member icon

Reputation: 121
  • View blog
  • Posts: 671
  • Joined: 01-October 08

Re: How do I check if two Status elements are the same?

Posted 17 December 2018 - 07:05 AM

I'd use XElement and LINQ. Not exactly sure what you are testing for but here is something to look at, a starting point.

        Dim xe As XElement
        'to load from path or other URI
        ' xe = XElement.Load("URI / path here")

        'for testing use literal
        xe = <Integration>
                 <ProtectionOrder>
                     <Statuses>
                         <Status Op="A">
                             <Current>true</Current>
                             <Active>No</Active>
                             <Date Op="A">12/13/2018</Date>
                             <Type Op="A" Word="EXPIRED">Expired</Type>
                         </Status>
                         <Status>
                             <Current>false</Current>
                             <Active>Yes</Active>
                             <Date>12/13/2016</Date>
                             <Type Word="SBJO">Signed By Judicial Officer</Type>
                         </Status>
                         <Status>
                             <Current>false</Current>
                             <Active>No</Active>
                             <Date>12/13/2016</Date>
                             <Type Word="DRAFT">Draft</Type>
                         </Status>
                     </Statuses>
                     <MNProtectionOrderAdditional>
                         <Statuses>
                             <Status Op="A">
                                 <Current>false</Current>
                                 <Active>No</Active>
                                 <Date Op="A">12/13/2018</Date>
                                 <Type Op="A" Word="EXPIRED">Expired</Type>
                             </Status>
                             <Status Op="E">
                                 <Current>false</Current>
                                 <Active>Yes</Active>
                                 <Date>12/13/2016</Date>
                                 <Type Word="SBJO">Signed By Judicial Officer</Type>
                             </Status>
                             <Status>
                                 <Current>true</Current>
                                 <Active>No</Active>
                                 <Date>12/13/2016</Date>
                                 <Type Word="DRAFT">Draft</Type>
                             </Status>
                         </Statuses>
                     </MNProtectionOrderAdditional>
                 </ProtectionOrder>
             </Integration>

        'use LINQ to select elsements
        Dim po As IEnumerable(Of XElement)
        po = From st In xe.<ProtectionOrder>.<Statuses>...<Status>
             Where [email protected] = "A" OrElse [email protected] = "E"
             Select st Take 1

        Dim poadd As IEnumerable(Of XElement)
        If po.Count = 1 Then
            poadd = From st In xe.<ProtectionOrder>.<MNProtectionOrderAdditional>.<Statuses>...<Status>
                    Where [email protected] = po(0)[email protected]
                    Select st Take 1
        End If


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1