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

前端之家收集整理的这篇文章主要介绍了如何从xml doc返回所有元素?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
从我的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>
@H_301_6@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
我建议您使用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.

猜你在找的XML相关文章