【问题标题】:Looping XML with element using xmlReader使用 xmlReader 循环带有元素的 XML
【发布时间】:2013-04-12 20:43:46
【问题描述】:
   Dim client As New WebClient()
   Dim xmlString As String = client.DownloadString("http://api.rovicorp.com/TVlistings/v9/listings/gridschedule/80000/info?locale=en-US&duration=220&includechannelimages=1&format=xml&apikey=" & api_TV)
   Dim counter As Integer = 0

Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString))
    Dim tvListings As XDocument = XDocument.Parse(xmlString)

    For Each blah As XElement In tvListings.Root.Elements
        counter += 1
    Next

    Debug.Print(counter)
End Using

我只得到一个 8 的 counter,应该是 100+ 左右。

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="TV-PG@V" 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="TV-PG@V" 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="TV-14@V" 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>

【问题讨论】:

  • 您确定您的文档中有超过 8 个 &lt;GridChannels&gt; 元素吗?请记住,Elements() 方法仅返回当前元素的直接子元素,因此您计算的是 Root 元素的直接子元素的数量,即 XML 中的 GetGridScheduleResult
  • xmlstring 是从哪里神奇地出现的?
  • @TonyHopkinson 更新了 OP 以显示这一点。
  • @MarcinJuraszek 积极。它是一个拥有 300 多个频道的电视节目。
  • 也许您可以Debug.Print() 您成功计数的 8 个节点来获取更多症状信息。

标签: xml vb.net xml-parsing xmlreader


【解决方案1】:

如果您只想计算 &lt;GridChannel&gt; 元素,那么您只需要一个小的 LINQ 语句

c#

XDocument document = XDocument.Parse(XmlString);
var count = document.Descendants("GridChannel").Count();

VB.Net

Dim document as XDocument = XDocument.Parse(XmlString)
Dim count = document.Descendants("GridChannel").Count()

【讨论】:

  • 计数为 0。 Dim count = blah.Descendants("GridChannel").Count()
  • 会不会是因为GridChannel没有这样设置及其
  • 没有属性和子元素应该无关紧要。您的 XML 是否与上述相同,包括大小写?我对上述内容进行了测试并得到了结果。如果您从文件而不是字符串变量加载,请使用 XDocument.Load("pathtofile")
  • 我仍然想出 0 来计数。 Dim tvListings As XDocument = XDocument.Load("c:\temp\xml.xml") Dim count = tvListings.Descendants("GridChannel").Count()
  • 您的 xml 格式是否正确?包括顶部的 XML 声明?
【解决方案2】:

那就试试这个

Dim doc as XmlDocument =  new XmlDocument();
doc.LoadXml(xmlString)
XmlNode channelsNode = doc.documentElement.SelectSingleNode("GridChannels")
foreach XmlNode gridChannelNode in channelsNode.SelectNodes("GridChannel"))
///
Next

鉴于您发布的内容,这应该只是获取 GridChannels 节点然后循环遍历它的 GridChannel 子节点的问题。

原谅缺乏VBness,我是C#男孩

【讨论】:

  • 有一些事情你没有告诉我们。调试时 channelsNode.OuterXml 显示什么?
【解决方案3】:

通过这样做得到它:

    Dim jsonObject As RootObject = JsonConvert.DeserializeObject(Of RootObject)(s)

    For Each post In jsonObject.GridScheduleResult.GridChannels
        Dim Channel As String = post.Channel
        Dim DisplayName As String = post.DisplayName

        If post.ChannelImages.Count <> 0 Then
            Dim ImageUrl As String = post.ChannelImages.Item(0).ToString
            Dim tmpL As Integer = InStr(ImageUrl, ":")
            Dim tmpR As Integer = InStr(ImageUrl, ",")
        End If

        While counter < post.Airings.Count
            Dim Title As String = post.Airings.Item(counter).Title
            Dim EpisodeTitle As String = post.Airings.Item(counter).EpisodeTitle
        End While

        theTime = 0
        counter = 0
    Next

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多