使用XmlDocument读取XML值

时间:2013-06-12 04:44:29

标签: xml vb.net

我想读取这个xml的值。 例如,获取* ram:Receive_Time *。

的值
<?xml version="1.0"?>
<rsm:BidNoticeInformationDocumentResponse xmlns:rsm="urn:mn:org:eppd:data:standard:BidNoticeInformationDocumentResponse:1" xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:9" xmlns:ram="urn:mn:org:eppd:data:standard:ReusableAggregateBusinessInformationEntity:1" xmlns:sbd="urn:mn:org:eppd:data:standard:BidNoticeInformationResponse:1" xmlns:bdh="urn:mn:org:eppd:data:standard:BusinessDocumentHeaderSchemaModule:1" xmlns:qdt="urn:un:unece:uncefact:data:standard:QualifiedDataType:8" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:mn:org:eppd:data:standard:BidNoticeInformationDocumentResponse:1..\Schema\DocSchema\BidNoticeInformationProxy.xsd">
  <bdh:BusinessDocumentHeader>
    <bdh:ResultDocument>
      <ram:Receive_Time>2013-06-12 11:03:06</ram:Receive_Time>
      <ram:Result_Code>0000</ram:Result_Code>
      <ram:Result_Message>SUCCESS</ram:Result_Message>
      <ram:TransmissionID>K20130612110301SSSSS</ram:TransmissionID>
    </bdh:ResultDocument>
  </bdh:BusinessDocumentHeader>
  <sbd:BidNoticeInformationResponse>
    <sbd:BidNoticeDocument>
      <ram:Name>Test Bid(Bid Number : 20110300001-00)</ram:Name>
      ....

我试过这个方法。但价值变得空虚。

Dim xmlDoc As New XmlDocument()
        xmlDoc.Load("BidNoticeInformationResponse.xml")
        'MsgBox(xmlDoc.InnerXml)

        Dim sr As New System.IO.StringReader(xmlDoc.InnerXml)
        Dim doc As New Xml.XmlDocument
        doc.Load(sr)
        Dim reader As New Xml.XmlNodeReader(doc)

        While reader.Read()
            If (reader.IsStartElement) Then
                If (reader.Prefix & ":" & reader.LocalName = "ram:Receive_Time") Then
                    MsgBox(reader.Value)
                End If
            End If
        End While

2 个答案:

答案 0 :(得分:1)

试试这个(未经测试):

Sub Main()
    Dim xml = XDocument.Load("YourXmlDoc")
    Console.WriteLine("ram:Receive_Time: " & xml.<rsm:BidNoticeInformationDocumentResponse>.<bdh:BusinessDocumentHeader>.<bdh:ResultDocument>.<ram:Receive_Time>.Value)    
End Sub

并查看doumentation

答案 1 :(得分:0)

在这种情况下,我得到了答案。它工作正常。

While reader.Read()

Dim type = reader.NodeType

If (type = XmlNodeType.Element) Then

If (reader.Prefix & ":" & reader.LocalName = "bdh:ResultDocument") Then
reader.ReadStartElement()
If (reader.Name = "ram:Receive_Time") Then
lblReceiveTime.Text = reader.ReadInnerXml.ToString.Trim
End If

If (reader.Name = "ram:Result_Code") Then
lblResultCode.Text = reader.ReadInnerXml.ToString.Trim
End If

If (reader.Name = "ram:Result_Message") Then
lblResultMessage.Text = reader.ReadInnerXml.ToString.Trim
End If

If (reader.Name = "ram:TransmissionID") Then
lblTransmissionID.Text = reader.ReadInnerXml.ToString.Trim
End If
End If
....