如何从 xml 文档返回所有元素? [英] How do I return all elements from a xml doc?
问题描述
从我的 xml 文档中,我想显示所有 ns1:Statute 节点.我的 vb 代码只显示一个节点 而不是全部 3 个.如何显示所有 3 节点及其元素?我知道我需要使用 selectNodes 而不是选择单个节点,然后使用 for each 进行循环.但我不知道该怎么做.XML
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 代码
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 文件中获取所有数据,它会更简单.不过,您必须创建一个类来反序列化数据.创建类 使用 Visual Studio 2010 创建 XML 架构并从中自动生成可序列化的类.使用 XmlSerializer https://msdn.microsoft.com/fr-fr/library/system.xml.serialization.xmlserializer(v=vs.110).aspx.
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.
这篇关于如何从 xml 文档返回所有元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!