2013-04-12 17 views
0

獲取XML元素我有下面的代碼:從響應

Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString)) 
    reader.ReadToFollowing("GridChannel") 
    Dim Channel As String = reader.GetAttribute("Channel") 
    Dim DisplayName As String = reader.GetAttribute("DisplayName") 

    reader.ReadToFollowing("Airings") 

    While reader.ReadToFollowing("GridAiring") 
     Dim Title As String = reader.GetAttribute("Title") 
     Dim EpisodeTitle As String = reader.GetAttribute("EpisodeTitle") 
     Dim AiringTDurationime As String = reader.GetAttribute("AiringTDurationime") 
     Dim isHD As Boolean = Convert.ToBoolean(reader.GetAttribute("isHD")) 
     Dim TVRating As String = reader.GetAttribute("TVRating") 
    End While 

    reader.MoveToElement() 
    reader.ReadToFollowing("ImageGrid") 
    Dim ImageUrl As String = reader.GetAttribute("ImageUrl") 

End Using 

我的XML看起來像這樣:

<GetGridScheduleResult xmlns="http://api.rovicorp.com/v9/listings" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Locale="en-US" ServiceId="5122" Name="Cityhere - Comcast" StartDate="2013-04-12T14:18:24.2054325Z" Duration="240"> 
<GridChannels> 
    <GridChannel ServiceId="890138" SourceId="1280" Order="20002" Channel="2" CallLetters="WGNAMER" DisplayName="WGNAMER" SourceLongName="WGN America" Type="24-Hours" SourceType="Basic" ParentNetworkId="0" IconAvailable="false" IsChannelOverride="false" SourceAttributes="0"> 
    <ChannelSchedules/> 
    <SourceAttributeTypes/> 
    <Airings> 
     <GridAiring ProgramId="35951" SeriesId="3490" Title="Matlock" EpisodeTitle="Santa Claus" AiringTime="2013-04-12T14:00:00Z" Duration="60" Color="Color" AiringType="Unknown" CC="true" LetterBox="false" Stereo="false" HD="false" SAP="false" TVRating="TV-PG" Dolby="false" DSS="false" HDLevel="HD Level Unknown" DVS="false" Category="Other" Subcategory="drama" Sports="false"/> 
     <GridAiring ProgramId="828869" SeriesId="1409" Title="In the Heat of the Night" EpisodeTitle="Hatton's Turn" AiringTime="2013-04-12T15:00:00Z" Duration="60" Color="Color" AiringType="Unknown" CC="true" LetterBox="false" Stereo="false" HD="false" SAP="false" TVRating="[email protected]" Dolby="false" DSS="false" HDLevel="HD Level Unknown" DVS="false" Category="Other" Subcategory="crime drama" Sports="false"/> 
     <GridAiring ProgramId="978338" SeriesId="1409" Title="In the Heat of the Night" EpisodeTitle="Hatton's Turn" AiringTime="2013-04-12T16:00:00Z" Duration="60" Color="Color" AiringType="Unknown" CC="true" LetterBox="false" Stereo="false" HD="false" SAP="false" TVRating="[email protected]" Dolby="false" DSS="false" HDLevel="HD Level Unknown" DVS="false" Category="Other" Subcategory="crime drama" Sports="false"/> 
     <GridAiring ProgramId="4210626" Title="WGN Midday News" AiringTime="2013-04-12T17:00:00Z" Duration="60" Color="Color" AiringType="New" CC="true" LetterBox="false" Stereo="true" HD="false" SAP="false" TVRating="None" Dolby="false" DSS="false" HDLevel="HD Level Unknown" DVS="false" Category="News" Subcategory="newscast" Sports="false"/> 
     <GridAiring ProgramId="878716" SeriesId="1028666" Title="Walker, Texas Ranger" EpisodeTitle="El Coyote, Part 2" AiringTime="2013-04-12T18:00:00Z" Duration="60" Color="Color" AiringType="Unknown" CC="true" LetterBox="false" Stereo="true" HD="false" SAP="false" TVRating="[email protected]" Dolby="false" DSS="false" HDLevel="HD Level Unknown" DVS="false" Category="Other" Subcategory="crime drama" Sports="false"/> 
    </Airings> 
    <ChannelImages> 
     <ImageGrid ImageUrl="http://cps-static.rovicorp.com/2/Open/TV%20Guide%20Widget%20Logos/WGN_2010.png" ImageId="427700" ImageTitle="WGN America" ImageCaption="Widget Logo" ObjectId="1280" ObjectName="WGN America" ImageCreditDisplay="false" ImageType="Station Logo" ImageHorizontalResolution="92" ImageVerticalResolution="36" ImageFormatId="0" AspectRatio="5:2" ParentImageId="16818227"> 
      <ObjectType>Source</ObjectType> 
      <ImageFormat xsi:nil="true"/> 
      <ImageExpiryDateTime xsi:nil="true"/> 
      <LastUpdate>2012-01-24T15:20:46.453Z</LastUpdate> 
     </ImageGrid> 
    </ChannelImages> 
    </GridChannel> 
    etc etc... 
</GridChannels> 
</GetGridScheduleResult> 

我得到沒有作爲的ImageUrl的值。我錯過了什麼,以深入到這個元素並獲得這些值?

回答

1

這是因爲以下循環:

While reader.ReadToFollowing("GridAiring") 
    Dim Title As String = reader.GetAttribute("Title") 
    Dim EpisodeTitle As String = reader.GetAttribute("EpisodeTitle") 
    Dim AiringTDurationime As String = reader.GetAttribute("AiringTDurationime") 
    Dim isHD As Boolean = Convert.ToBoolean(reader.GetAttribute("isHD")) 
    Dim TVRating As String = reader.GetAttribute("TVRating") 
End While 

讀取最後GridAiring元素後的文件到最終被發現。

試一下:

Using reader As XmlReader = XmlReader.Create(New StreamReader("Input.txt")) 
    reader.ReadToFollowing("GridChannel") 
    Dim Channel As String = reader.GetAttribute("Channel") 
    Dim DisplayName As String = reader.GetAttribute("DisplayName") 

    reader.ReadToFollowing("Airings") 

    While reader.Read() AndAlso Not reader.NodeType = XmlNodeType.Element 
    End While 

    While reader.Name = "GridAiring" 
     Dim Title As String = reader.GetAttribute("Title") 
     Dim EpisodeTitle As String = reader.GetAttribute("EpisodeTitle") 
     Dim AiringTDurationime As String = reader.GetAttribute("AiringTDurationime") 
     Dim isHD As Boolean = Convert.ToBoolean(reader.GetAttribute("isHD")) 
     Dim TVRating As String = reader.GetAttribute("TVRating") 

     reader.ReadToNextSibling("GridAiring") 
    End While 

    reader.MoveToElement() 
    reader.ReadToFollowing("ImageGrid") 
    Dim ImageUrl As String = reader.GetAttribute("ImageUrl") 

End Using 
+0

你猜對了,MarcinJuraszek!謝謝! – StealthRT