none
How do I use For loop to check each node and import them to a new document? RRS feed

  • Question

  • In my function I would like to use a For loop to get all the statutes (xml) inside the object objXmlBcaResponseDoc. In my case there are 2 statutes. I would like the output to look like the one I have posted here below. I am not sure how to do the For loop. The commented For loop is from another function but it is not working inside this function.

    The output is added into the **objXmlResponseMessageDoc** object and should look like this with 2 statutes (ns1:Statute) and a totalCount=2

    <BasicSearchQueryResponse xmlns="http://crimnet.state.mn.us/mnjustice/statute/service/4.0">
    	<StatutesXml>
    		<Statutes runType="Request" runDateTime="2015-03-17T10:23:04" totalCount="2">
    			<ns1:Statute xmlns:ns1="http://crimnet.state.mn.us/mnjustice/statute/messages/4.0">
    				<StatuteId xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">8471</StatuteId>
    				<Chapter xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">60</Chapter>
    				<Section xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">55</Section>
    				<Subdivision xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0"/>
    			</ns1:Statute>
    			<ns1:Statute>
    				<StatuteId xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">9722</StatuteId>
    				<Chapter xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">90</Chapter>
    				<Section xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">25</Section>
    				<Subdivision xsi:nil="true" xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0"/>
    			</ns1:Statute>
    		</Statutes>
    	</StatutesXml>
    </BasicSearchQueryResponse>

    My xml doc is found inside objXmlBcaResponseDoc Here is xml inside objXmlBcaResponseDoc object

    <BasicSearchQueryResponse xmlns="http://crimnet.state.mn.us/mnjustice/statute/service/4.0">
    	<ns1:Statutes xmlns:ns1="http://crimnet.state.mn.us/mnjustice/statute/messages/4.0">
    		<ns1:Statute>
    			<StatuteId xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">8471</StatuteId>
    			<Chapter xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">60</Chapter>
    			<Section xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">55</Section>
    			<Subdivision xsi:nil="true" xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0"/>
    		</ns1:Statute>
    		<ns1:Statute>
    			<StatuteId xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">9722</StatuteId>
    			<Chapter xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">90</Chapter>
    			<Section xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">25</Section>
    			<Subdivision xsi:nil="true" xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0"/>
    		</ns1:Statute>
    </BasicSearchQueryResponse>

    Here is 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"))
    
                'Build the initial response document
                objXmlResponseMessageDoc = New XmlDocument
                'Add the first element into the document GetStatutesResponse with its namespace
                objXmlResponseMessageDoc.AppendChild(objXmlResponseMessageDoc.CreateElement("GetStatutesResponse", "http://www.courts.state.mn.us/StatuteService/1.0"))
                'Add a child node to the GetStatutesResponse node
                objXmlResponseMessageDoc.SelectSingleNode("ss:GetStatutesResponse", aobjXMLNameSpaceManager).AppendChild(objXmlResponseMessageDoc.CreateElement("StatutesXml", "http://www.courts.state.mn.us/StatuteService/1.0"))
                objXmlResponseMessageDoc.SelectSingleNode("ss:GetStatutesResponse/ss:StatutesXml", aobjXMLNameSpaceManager).AppendChild(objXmlResponseMessageDoc.CreateElement("Statutes", "http://www.courts.state.mn.us/StatuteService/1.0"))
                'Convert the node Statutes into an element and set the runType attribute (runType="Request") by adding it's value Request 
                CType(objXmlResponseMessageDoc.SelectSingleNode("ss:GetStatutesResponse/ss:StatutesXml/ss:Statutes", aobjXMLNameSpaceManager), System.Xml.XmlElement).SetAttribute("runType", "Request")
                'Convert the node Statutes into an element and set the attribute (runDateTime="2015-03-05T10:29:40") by adding it 
                CType(objXmlResponseMessageDoc.SelectSingleNode("ss:GetStatutesResponse/ss:StatutesXml/ss:Statutes", aobjXMLNameSpaceManager), System.Xml.XmlElement).SetAttribute("runDateTime", Format(Now, "yyyy-MM-ddTHH:mm:ss"))
    
    			'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")))
    
                'Uncomment last working section below
                objXmlRequestMessageDoc.DocumentElement.SelectSingleNode("ns1:BasicSearchCriteria/st:Chapter", aobjXMLNameSpaceManager).InnerText = aobjXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:Chapter", 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) 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
                '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
    			'This is where I want to use a For loop to check for the statutes found using the Chapter
    			
    			'Loop through each Id
                'For Each objXmlStatuteIdNode In aobjXmlGetStatuteRequestNode.SelectNodes("ss:Statute/ss:StatuteId/ss:Id[string-length(.)>0]", aobjXMLNameSpaceManager)
                    'Create the BCA request message
                    'objXmlRequestMessageDoc = New XmlDocument
                    'objXmlRequestMessageDoc.AppendChild(objXmlRequestMessageDoc.CreateElement("ns:SingleStatuteRequest", aobjXMLNameSpaceManager.LookupNamespace("ns")))
    				
                    'objXmlRequestMessageDoc.SelectSingleNode("ns:SingleStatuteRequest", aobjXMLNameSpaceManager).AppendChild(objXmlRequestMessageDoc.CreateElement("ns:statuteId", aobjXMLNameSpaceManager.LookupNamespace("ns")))
    				
                    'objXmlRequestMessageDoc.DocumentElement.SelectSingleNode("ns:statuteId", aobjXMLNameSpaceManager).InnerText = objXmlStatuteIdNode.InnerText aobjBroker.PostMessageWarehouseSnapshot(objXmlRequestMessageDoc.OuterXml, "Request Message", 1)
    
                    'intCount = intCount + 1
                    'objXmlBcaResponseDoc = New XmlDocument
    				'File name is BCASearchQueryResponse.xml
                    'objXmlBcaResponseDoc.Load("\\j00000swebint\mscapps\deve\appfiles\temp\BCASearchQueryResponse.xml")
                    'objXmlResponseMessageDoc.DocumentElement.SelectSingleNode("ss:StatutesXml/ss:Statutes", aobjXMLNameSpaceManager).AppendChild(objXmlResponseMessageDoc.ImportNode(objXmlBcaResponseDoc.DocumentElement.SelectSingleNode("ns1:Statute", aobjXMLNameSpaceManager), True))
                'Next
                'Count how many Statute nodes found
                CType(objXmlResponseMessageDoc.SelectSingleNode("ss:BasicSearchQueryResponse/ss:StatutesXml/ss:Statutes", aobjXMLNameSpaceManager), System.Xml.XmlElement).SetAttribute("totalCount", CStr(intCount))
    
                Return objXmlResponseMessageDoc
            End Function

    Tuesday, March 17, 2015 4:50 PM

Answers

  • I agree with you. For now I don't know how to resolve this issue though and wont get permission to use LINQ

    Your boss is a clown I would never want to work with or for a he or she. This is what Linq-2-XML or Xpath are made for to reslove these kinds of XML issues.

    Maybe you should give the clown a demo so that he or she sees the power of the two technologies and pull the wool off of his or her eyes.

    • Marked as answer by winkimjr2 Wednesday, March 18, 2015 5:13 PM
    Wednesday, March 18, 2015 1:28 PM

All replies

    • Proposed as answer by Frank L. Smith Tuesday, March 17, 2015 7:06 PM
    Tuesday, March 17, 2015 5:20 PM
  • That sounds good however, I am not required to use LINQ
    Tuesday, March 17, 2015 6:08 PM
  • That sounds good however, I am not required to use LINQ

    Huh? What do you mean by that? Linq is a viable solution that's using less code than what you have there.

    Also there is Xpath that would use less code than what you have there. The below is a C# example. but you should be able to find something in VB.

    http://www.codeproject.com/Articles/9494/Manipulate-XML-data-with-XPath-and-XmlDocument-C

    Maybe, it somthing you can use in the future as both technologies come with using .NET.

    Tuesday, March 17, 2015 7:00 PM
  • Maybe you should learn how to usee Linq-2-XML.

    http://www.codeproject.com/Articles/400269/How-to-Start-with-XML-and-LINQ-A-Beginner-Guide


    I have been suggesting this to him for a while now. I think you're fighting an uphill battle convincing him, but for what it's worth -- I agree with you.

    Still lost in code, just at a little higher level.

    :-)

    Tuesday, March 17, 2015 7:06 PM
  • I agree with you. The only issue is that the code I am writing and or my company does not use LINQ.  My boss wont allow it.
    Tuesday, March 17, 2015 7:36 PM
  • I agree with you. The only issue is that the code I am writing and or my company does not use LINQ.  My boss wont allow it.

    How sad is that? Is your boss riding into work using a horse and buggy too? :)
    Tuesday, March 17, 2015 7:40 PM
  • Guys I know you are trying to help me. However I am not allowed to use what you are suggesting. They want me to use For loop. It's not as easy as it may sound. 
    Tuesday, March 17, 2015 7:43 PM
  • Here is a Microsoft link showing the method of LINQ in a more organized fashion in C# and in VB

    https://msdn.microsoft.com/en-us/library/bb397897.aspx

    Tuesday, March 17, 2015 8:09 PM
  • LINQ would make this job easier and I think it is optimizer than manual process you are working, a win on both sides.
    Tuesday, March 17, 2015 11:31 PM
  • I agree with you. The only issue is that the code I am writing and or my company does not use LINQ.  My boss wont allow it.
    I did not know this. I think this would be a good opportunity to get permission to start using POST-1990 development methods.
    Tuesday, March 17, 2015 11:32 PM
  • I agree with you. For now I don't know how to resolve this issue though and wont get permission to use LINQ
    Wednesday, March 18, 2015 1:13 PM
  • I agree with you. For now I don't know how to resolve this issue though and wont get permission to use LINQ

    Your boss is a clown I would never want to work with or for a he or she. This is what Linq-2-XML or Xpath are made for to reslove these kinds of XML issues.

    Maybe you should give the clown a demo so that he or she sees the power of the two technologies and pull the wool off of his or her eyes.

    • Marked as answer by winkimjr2 Wednesday, March 18, 2015 5:13 PM
    Wednesday, March 18, 2015 1:28 PM
  • I will start to learn it on my own. Since you and other have emphasized the power of LINQ, it is worth checking it out. 
    Wednesday, March 18, 2015 5:50 PM
  • I will start to learn it on my own. Since you and other have emphasized the power of LINQ, it is worth checking it out. 

    LINQ is quite powerful, whether it's LINQ-To-Object, LINQ-To-XML, LINQ-To-SQL or any that I missed.

    It's name means "Language Integrated Query" for a reason: Essentially you 'talk' to it using it's language (based on your language, whether it's VB or C#, etc.) and it then 'talks' to the recipient in a way that it understands. You don't have to understand the underlying part of it, only what's needed to get LINQ to perform what you want.

    I like LINQ and use it a lot BUT I will add this anyway:

    It uses deferred execution - when the code gets to your definition of what to do, it's not then performed. It's peformed when a request to show the results occurs.

    It either works ... or not. You can't debug LINQ. That can, as you might expect, have some downsides, so my advice (despite what many here will say to the contrary) is to not compose long, involved LINQ queries; break them up into sections and let each do whatever that part of it does.

    If it doesn't work then you at least then know where to look. ;-)

    LINQ was introduced in framework version 3.5 (VS 2008) so it's not new at all.

    Once you get the hang of it, you won't want to do without it but of course there's a learning curve (as with anything new).


    Still lost in code, just at a little higher level.

    :-)

    Wednesday, March 18, 2015 6:02 PM
  • I will start to learn it on my own. Since you and other have emphasized the power of LINQ, it is worth checking it out. 

    You may want ot look at Xpath too, which I was just recently introduced to/forced to use it. After a quick tutorial on it, which is not the one below, I realized just how powerful Xpath is. I had heard about Xpath, but I always used Linq. Maybe, you'll find a VB.Net tutorial on Xpath.

    http://www.codeproject.com/Articles/9494/Manipulate-XML-data-with-XPath-and-XmlDocument-C

    Either way Linq or Xpath, you can't go wrong.

    Wednesday, March 18, 2015 6:25 PM
  • You may want ot look at Xpath too, which I was just recently introduced to/forced to use it. After a quick tutorial on it, which is not the one below, I realized just how powerful Xpath is. I had heard about Xpath, but I always used Linq. Maybe, you'll find a VB.Net tutorial on Xpath.

    http://www.codeproject.com/Articles/9494/Manipulate-XML-data-with-XPath-and-XmlDocument-C

    Either way Linq or Xpath, you can't go wrong.

    What is XPath and what does it do that you're impressed with?

    Yes, I see your link, but give me the abbreviated elevator speech on what it is please. It has me curious.


    Still lost in code, just at a little higher level.

    :-)

    Wednesday, March 18, 2015 6:34 PM
  • What is XPath and what does it do that you're impressed with?

    Yes, I see your link, but give me the abbreviated elevator speech on what it is please. It has me curious.


    http://searchsoa.techtarget.com/definition/XPath

    http://www.techrepublic.com/article/easily-navigate-xml-with-vbnet-and-xpath/

    http://www.techrepublic.com/article/using-xpath-string-functions-in-xslt-templates/

    The way that all this is being used by me now on a project is the HTML controls on the screen are built by XML not only about what the controls are and their attributes,   but the data from from the database is XML too with both going through transfermations vis XSLT as the HTML controls are built dynamically by XML data for the controls and the XML database data with decision being made in the transfermation on the fly.

    There are many usages with Xpath not just this one I am talking about with Xpath. You can do the same kind of thing with XAML and WPF forms as they are dynamically built. But it goes well beyond what I am talking about and the usage of Xpath. Xpath 3.0 is the latest version. 

    http://www.balisage.net/Proceedings/vol10/html/Novatchev01/BalisageVol10-Novatchev01.html

    Wednesday, March 18, 2015 7:42 PM
  • What is XPath and what does it do that you're impressed with?

    Yes, I see your link, but give me the abbreviated elevator speech on what it is please. It has me curious.


    http://searchsoa.techtarget.com/definition/XPath

    http://www.techrepublic.com/article/easily-navigate-xml-with-vbnet-and-xpath/

    http://www.techrepublic.com/article/using-xpath-string-functions-in-xslt-templates/

    The way that all this is being used by me now on a project is the HTML controls on the screen are built by XML not only about what the controls are and their attributes,   but the data from from the database is XML too with both going through transfermations vis XSLT as the HTML controls are built dynamically by XML data for the controls and the XML database data with decision being made in the transfermation on the fly.

    There are many usages with Xpath not just this one I am talking about with Xpath. You can do the same kind of thing with XAML and WPF forms as they are dynamically built. But it goes well beyond what I am talking about and the usage of Xpath. Xpath 3.0 is the latest version. 

    http://www.balisage.net/Proceedings/vol10/html/Novatchev01/BalisageVol10-Novatchev01.html

    Thanks - I'll look into that at some point.

    Still lost in code, just at a little higher level.

    :-)

    Thursday, March 19, 2015 3:23 AM