获取XML节点值

时间:2014-04-09 18:17:25

标签: xml vb.net

我无法从以下文档中获取XML值。

我已多次尝试过,但我无法让它发挥作用。

Dim tr As IO.TextReader = New IO.StreamReader("E:\response.xml")

'Do While xmlFile.Read()
Dim doc As XmlDocument = New XmlDocument()
doc.LoadXml(tr.ReadToEnd)
For Each eventNode As XmlNode In doc.SelectNodes("ResponseRecord")
    If xmlFile.NodeType = XmlNodeType.Element AndAlso xmlFile.Name = "AddressLine1" Then
        Console.WriteLine(xmlFile.GetAttribute(0))
        Console.WriteLine(xmlFile.a("AddressLine1").ToString)
    End If
Next

XML DOC:

<?xml version="1.0" encoding="UTF-8"?>
<Response xmlns  ="http://schemas.xxxxxxxx.org/2004/07/WcfServiceMD.mdContactVerify" 
          xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Records>
        <ResponseRecord>
            <AddressDeliveryInstallation />
            <AddressExtras />
            <AddressHouseNumber>320</AddressHouseNumber>
            <AddressKey>00009611820</AddressKey>
            <AddressLine1>320 Wayward Ave</AddressLine1>
            <AddressLine2 />
            <AddressLockBox />
            <AddressPostDirection />
            <AddressPreDirection />
            <AddressPrivateMailboxName />
            <AddressPrivateMailboxRange />
            <AddressRouteService />
            <AddressStreetName>Hayward</AddressStreetName>
            <AddressStreetSuffix>Ave</AddressStreetSuffix>
            <AddressSuiteName />
            <AddressSuiteNumber />
            <AddressTypeCode>S</AddressTypeCode>
            <AreaCode>313</AreaCode>
            <CBSACode>40380</CBSACode>
            <CBSADivisionCode />
            <CBSADivisionLevel />
            <CBSADivisionTitle />
            <CBSALevel>Metropolitan Statistical Area</CBSALevel>
            <CBSATitle>Rochester, NY</CBSATitle>
            <CarrierRoute>C009</CarrierRoute>
            <CensusBlock>3009</CensusBlock>
            <CensusTract>005600</CensusTract>
            <City>Rochester</City>
            <CityAbbreviation>Rochester</CityAbbreviation>
            <CompanyName>Mike's Pizzerias</CompanyName>
            <CongressionalDistrict>25</CongressionalDistrict>
            <CountryCode>US</CountryCode>
            <CountryName>United States of America</CountryName>
            <CountyFIPS>11111</CountyFIPS>
            <CountyName>Monroe</CountyName>
            <DeliveryIndicator>R</DeliveryIndicator>
            <DeliveryPointCheckDigit>2</DeliveryPointCheckDigit>
            <DeliveryPointCode>20</DeliveryPointCode>
            <DomainName />
            <EmailAddress />
            <Gender>N</Gender>
            <Gender2 />
            <Latitude>43.000000</Latitude>
            <Longitude>-77.00000</Longitude>
            <MailboxName />
            <NameFirst>Pipo</NameFirst>
            <NameFirst2 />
            <NameFull>Pipo Laun</NameFull>
            <NameLast>Pio</NameLast>
            <NameLast2 />
            <NameMiddle />
            <NameMiddle2 />
            <NamePrefix />
            <NamePrefix2 />
            <NameSuffix />
            <NameSuffix2 />
            <NewAreaCode />
            <PhoneNumber>999999999</PhoneNumber>
            <PhonePrefix>999</PhonePrefix>
            <PhoneSuffix>9999</PhoneSuffix>
            <PlaceCode>3883000</PlaceCode>
            <PlaceName>Rochester</PlaceName>
            <Plus4>9292</Plus4>
            <PostalCode>11111</PostalCode>
            <PrivateMailBox />
            <RecordExtras />
            <RecordID>1</RecordID>
            <Reserved />
            <Results>AS01,GS01,NS01,NS05,NS06,PS01,PS08,PS11</Results>
            <Salutation />
            <State>NY</State>
            <StateName>New York</StateName>
            <Suite />
            <TopLevelDomain />
            <UrbanizationName />
        </ResponseRecord>
    </Records>
    <TotalRecords>1</TotalRecords>
    <TransmissionReference>whateve.</TransmissionReference>
    <TransmissionResults />
    <Version>1</Version>
</Response>

1 个答案:

答案 0 :(得分:1)

您需要使用XmlNamespaceManager,因为您的XML文档确实具有名称空间:

Dim doc As XmlDocument = New XmlDocument()
Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("ns", "http://schemas.xxxxxxxx.org/2004/07/WcfServiceMD.mdContactVerify")

doc.Load("E:\response.xml")
For Each record As XmlElement In doc.SelectNodes("/ns:Response/ns:Records/ns:ResponseRecord", nsmgr)
    Console.WriteLine(record.SelectSingleNode("ns:AddressLine1", nsmgr).InnerText)
Next