如何从xml doc返回所有元素?

时间:2015-02-17 16:49:30

标签: xml vb.net

从我的xml文档中,我想显示所有ns1:Statute节点。 我的vb代码只显示一个节点 而不是全部3个节点。 如何使用其元素显示所有3个 节点?我知道我需要使用selectNodes而不是选择单个节点,然后用于每个循环。但我不知道该怎么做。 的 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代码

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("\\j00000swebint\mscapps\deve\appfiles\temp\BcaStatutes.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

1 个答案:

答案 0 :(得分:0)

我建议您使用XmlSerializer,如果您想从xml文件中获取所有数据,它会更简单。但是,您必须创建一个类来反序列化数据。创建班级Creating XML schema and auto-generate the serializable class out of it using Visual Studio 2010。要使用XmlSerializer https://msdn.microsoft.com/fr-fr/library/system.xml.serialization.xmlserializer(v=vs.110).aspx