首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XmlReader环通过节点

XmlReader环通过节点
EN

Stack Overflow用户
提问于 2013-04-12 15:18:32
回答 3查看 2.2K关注 0票数 1

嘿,我只有以下代码:

代码语言:javascript
复制
    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")
        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 Using

上面的代码工作得很好,但是我在循环Airings部件时遇到了问题。

该部分的XML如下所示:

代码语言:javascript
复制
    <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>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-04-12 15:49:35

可以循环执行以下操作:

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2013-04-12 15:40:21

如果没有进入XML序列化,为什么不使用XmlReader.GetAttribute呢?

然后,您应该能够将代码缩减到以下内容:

代码语言:javascript
复制
Dim ServiceId As String = reader.GetAttribute("ServiceId")

依此类推,它的可读性更强,维护也更容易。

编辑:循环通过XML,我更喜欢这样的方式:

代码语言:javascript
复制
Dim Airings As XDocument = XDocument.Parse(xmlString)
For Each GridAiring As XElement In Airings.Root.Elements
  Dim ProgramId As String = GridAiring.Attribute("ProgramId").Value
  'read other properties here
Next
票数 2
EN

Stack Overflow用户

发布于 2013-04-13 10:33:34

与其循环浏览,不如使用LINQ获得更好的性能。下面是一个将ProgramId、SeriesId和Title拖到可枚举中的示例。

代码语言:javascript
复制
Dim document As XDocument = XDocument.Load("c:\tmp\test.xml")
Dim airings = From i In document.Descendants("Airings")
              Select New With
                     {Key .ProgramId = i.Attribute("ProgramId").Value,
                      Key .SeriesId = i.Attribute("SeriesId").Value,
                      Key .Title = i.Attribute("Title").Value}

然后,您可以循环通过可枚举。一个小例子就是在控制台中打印结果。

代码语言:javascript
复制
For Each a In airings
    Console.WriteLine(String.Format("{0},{1},{2}", a.ProgramId, a.SeriesId, a.Title))
    Console.Read()
Next

当然,对于每个项目,您只需调用.ProgramId.SeriesId等来获得值。

如果您想要添加检查以确保and没有缺少属性并防止可能的异常,您可以在select中添加检查以确保它的存在如下所示。

代码语言:javascript
复制
Dim document As XDocument = XDocument.Load("c:\tmp\test.xml")
Dim airings = From i In document.Descendants("Airings")
              Where i.Attribute("ProgramId") IsNot Nothing _
              And i.Attribute("SeriesId") IsNot Nothing _
              And i.Attribute("Title") IsNot Nothing
              Select New With
                     {Key .ProgramId = i.Attribute("ProgramId").Value,
                      Key .SeriesId = i.Attribute("SeriesId").Value,
                      Key .Title = i.Attribute("Title").Value}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15975092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档