Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

vb.net - How do I return all elements from a xml doc?

From my xml document, I want to display all ns1:Statute nodes. My vb code is only displaying one node and not all 3. How do I display all 3 nodes with their elements? I am aware I need to use selectNodes instead of select single node, and then use for each to loop through. But I am not sure how to do it. XML

   <ns1:Statutes xmlns:ns1="http://crimnet.state.mn.us/mnjustice/statute/messages/4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ns1:Statute>
        <StatuteId xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">12</StatuteId>
        <Chapter xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">171</Chapter>
        <Section xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">22</Section>
        <Subdivision xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">1(7)</Subdivision>
        <Year xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">1990</Year>
    </ns1:Statute>
    <ns1:Statute>
        <StatuteId xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">875</StatuteId>
        <Chapter xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">171</Chapter>
        <Section xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">22</Section>
        <Subdivision xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">1(7)</Subdivision>
        <Year xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">1995</Year>
    </ns1:Statute>
    <ns1:Statute>
        <StatuteId xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">75</StatuteId>
        <Chapter xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">171</Chapter>
        <Section xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">22</Section>
        <Subdivision xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">1(7)</Subdivision>
        <Year xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">1974</Year>
    </ns1:Statute>
</ns1:Statutes>

VB 2010 code

Public Class GetStatutes
    Shared Sub main()
        Dim objMessageProcessor As New MessageProcessor
        Dim objSchemasCollection As New Msc.Integration.MessageBroker.Library.v4.SchemasCollection
        Dim objTransformsCollection As New Msc.Integration.MessageBroker.Library.v4.TransformsCollection
        objMessageProcessor.ProcessInputQueue(False, False, objSchemasCollection, objTransformsCollection)
    End Sub

    'Child class MessageProcessor which inherits from main class GetStatutes
    Private Class MessageProcessor
        Inherits Msc.Integration.ServiceCatalog.Library.v4.SoapMessageProcessor
        Protected Overrides Sub ProcessMessage(ByRef aobjBroker As ServiceCatalog.Library.v4.Broker, ByRef aobjXMLInputSoapEnvelopeDoc As System.Xml.XmlDocument, ByRef aobjInstantiatedObjectsCollection As Microsoft.VisualBasic.Collection, ByRef aobjConsumer As ServiceCatalog.Library.v4.Consumer)
            MyBase.ProcessMessage(aobjBroker, aobjXMLInputSoapEnvelopeDoc, aobjInstantiatedObjectsCollection, aobjConsumer)

            Dim objXmlStatutesDoc As XmlDocument
            Dim objXmlStatuteNode As XmlNode
            Dim objNameTable As Xml.NameTable
            Dim objXMLNameSpaceManager As XmlNamespaceManager
            Dim objXmlBcaResponseDoc As XmlDocument
            Dim objXMLOutputSoapEnvelopeDoc As XmlDocument

            'set up the namespace manager
            objNameTable = New Xml.NameTable
            objXMLNameSpaceManager = New Xml.XmlNamespaceManager(objNameTable)
            objXMLNameSpaceManager.AddNamespace("soap", Msc.Integration.Utility.Library.v4.Soap.NamespaceUri(aobjBroker.SoapMessageVersion))
            objXMLNameSpaceManager.AddNamespace("wsa", Msc.Integration.Utility.Library.v4.Soap.WsaNamespaceUri(aobjBroker.SoapMessageVersion))
            objXMLNameSpaceManager.AddNamespace("ns1", "http://crimnet.state.mn.us/mnjustice/statute/messages/4.0")
            objXMLNameSpaceManager.AddNamespace("st", "http://crimnet.state.mn.us/mnjustice/statute/4.0")

            objXmlStatuteNode = aobjXMLInputSoapEnvelopeDoc.DocumentElement.SelectSingleNode("soap:Body/GetBCAStatuteRequest", objXMLNameSpaceManager)
            objXmlStatutesDoc = New XmlDocument

            'Get the statutes
            objXmlBcaResponseDoc = New XmlDocument
            objXmlBcaResponseDoc.Load("\j00000swebintmscappsdeveappfilesempBcaStatutes.xml")

            objXmlStatutesDoc = New XmlDocument
            objXmlStatutesDoc.AppendChild(objXmlStatutesDoc.CreateElement("Statutes"))
            objXmlStatutesDoc.DocumentElement.SetAttribute("runType", "Request")
            objXmlStatutesDoc.DocumentElement.SetAttribute("runDateTime", Format(Now, "yyyy-MM-ddTHH:mm:ss"))

            'Create a variable to store the statute element information ns1:Statute name space
            objXmlStatuteNode = objXmlBcaResponseDoc.DocumentElement.SelectSingleNode("ns1:Statute", objXMLNameSpaceManager)

            'Add the variable objXmlStatudeNode to the object objXmlStatuteDoc
            objXmlStatutesDoc.DocumentElement.AppendChild(objXmlStatutesDoc.ImportNode(objXmlStatuteNode, True))

            'Create the SOAP envelope to return the reply to the submitter
            objXMLOutputSoapEnvelopeDoc = aobjBroker.CreateSoapEnvelope("http://www.courts.state.mn.us/StatuteService/1.0/GetStatutesResponse", _
              Msc.Integration.Utility.Library.v4.Soap.GetReplyEndpointReference(aobjXMLInputSoapEnvelopeDoc), _
              objXmlStatutesDoc.DocumentElement, , aobjConsumer, _
              aobjXMLInputSoapEnvelopeDoc.DocumentElement.SelectSingleNode("soap:Header/wsa:MessageID", objXMLNameSpaceManager).InnerText)

            'Return the response to the requester
            aobjBroker.Reply(objXMLOutputSoapEnvelopeDoc)
        End Sub
    End Class
End Class
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I suggest you use the XmlSerializer it is way more simple if you want to get all the data from the xml file. You will have to create a class to deserialize the data in though. To create the class Creating XML schema and auto-generate the serializable class out of it using Visual Studio 2010. To use the XmlSerializer https://msdn.microsoft.com/fr-fr/library/system.xml.serialization.xmlserializer(v=vs.110).aspx.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...