使用xmlreader和vb.net解析带有嵌套节点的xml文件

时间:2011-12-29 18:39:34

标签: xml vb.net

我正在尝试解析这个包含一些嵌套节点的xml文件。我不知道如何把看起来整洁的xml文件内容放在这里。如果你能告诉我如何,那也会很棒。

这是我的代码。我能够检索一些信息,但其中一些不正确(strGrant具有应该用于strSupport的值)。 有人能告诉我我的代码有什么问题。我是新手。任何帮助表示赞赏!

    Dim strGrant, strSupport, strDatabank, strWeight, strNumOfGrants, strNumOfSupports, strNumOfDatabanks As String

    While XmlReader.Read()
        Console.WriteLine("{0}: {1}", XmlReader.NodeType.ToString(), XmlReader.Name)

        If XmlReader.Name.ToString() = "GrantInfo/NumberOfGrants" Then strNumOfGrants = Trim(XmlReader.ReadString())

        If XmlReader.Name.ToString() = "NumberOfGrants" Then strNumOfGrants = Trim(XmlReader.ReadString())

        If (XmlReader.Name.ToString() = "Grant/CLabelInfo") Then strGrant = XmlReader.ReadString()

        If XmlReader.Name.ToString() = "Name" Then strGrant = XmlReader.ReadString()
        '    ''End If

        If XmlReader.Name.ToString() = "CleanUpData/DatabankInfo/NumberOfDatabanks" Then strNumOfDatabanks = Trim(XmlReader.ReadString())

        If XmlReader.Name.ToString() = "NumberOfDatabanks" Then strNumOfDatabanks = Trim(XmlReader.ReadString())

        If XmlReader.Name.ToString() = "Databank/CLabelInfo" Then strDatabank = Trim(XmlReader.ReadString())

        If XmlReader.Name.ToString() = "Name" Then strDatabank = XmlReader.ReadString()

        If XmlReader.Name.ToString() = "Confidence" Then strWeight = XmlReader.ReadString()


        If XmlReader.Name.ToString() = "Name" And XmlReader.NodeType.ToString() = "EndElement" Then

            Response.Write("- Number of Grants: " & strNumOfGrants & "<br>")
            Response.Write("- Grant Numbers: " & strGrant & "<br>")         

        End If
        If XmlReader.Name.ToString() = "CleanUpData/SupportInfo/NumberOfSupports" Then strNumOfSupports = Trim(XmlReader.ReadString())
        If XmlReader.Name.ToString() = "NumberOfSupports" Then strNumOfSupports = Trim(XmlReader.ReadString())
        If XmlReader.Name.ToString() = "Support/CLabelInfo" Then strSupport = Trim(XmlReader.ReadString())
        If XmlReader.Name.ToString() = "Name" Then strSupport = Trim(XmlReader.ReadString())
        If XmlReader.Name.ToString() = "CLabelInfo" And XmlReader.NodeType.ToString() = "EndElement" Then
            Response.Write("- Number of Supports: " & strNumOfSupports & "<br>")
            Response.Write("- Support Number: " & strSupport & "<br>")

        End If
        If XmlReader.Name.ToString() = "CleanUpData/DatabankInfo/NumberOfDatabanks" Then strNumOfSupports = Trim(XmlReader.ReadString())
        If XmlReader.Name.ToString() = "NumberOfDatabanks" Then strNumOfDatabanks = Trim(XmlReader.ReadString())
        If XmlReader.Name.ToString() = "Databank/CLabelInfo" Then strDatabank = Trim(XmlReader.ReadString())
        If XmlReader.Name.ToString() = "Name" Then strDatabank = Trim(XmlReader.ReadString())
        If XmlReader.Name.ToString() = "CLabelInfo" And XmlReader.NodeType.ToString() = "EndElement" Then
            Response.Write("- Number of Databanks: " & strNumOfDatabanks & "<br>")
            Response.Write("- Databank Number: " & strDatabank & "<br>")

        End If
    End While
    XmlReader.Close()

这是我的结果:

赠款数量:1
- 拨款号码:DK30111
- 支持数量:
- 支持号码:
- 数据库数量:
- 数据库号码:
- 赠款数量:1
- 拨款号码:NIHExtra
- 支持数量:1
- 支持号码:
- 数据库数量:
- 数据库号码:
- 赠款数量:1
- 拨款号码:RefSeq / NM_007614
- 支持数量:1
- 支持号码:
- 数据库数量:
- 数据库号码:
- 赠款数量:1
- 拨款号码:RefSeq / NM_008084
- 支持数量:1
- 支持号码:
- 数据库数量:
- 数据库号码:
- 赠款数量:1
- 拨款号码:[未找到]
- 支持数量:1
- 支持号码:
- 数据库数量:
- 数据库号:

1 个答案:

答案 0 :(得分:0)

这是因为您正在错误地使用节点名称。节点名称仅包含当前节点的名称,而不包含名称层次结构。

因此,您需要更改名称声明以反映这一点。如果比较是在案例陈述中,它也会更有效率。最后,为了解释文件大小的波动,你应该比较大写或小写。

以下是一个例子:

Select Case XmlReader.Name.ToString().ToLower
    Case "numberofgrants" 
        strNumOfGrants = Trim(XmlReader.ReadString())

End Select

您应该能够将其余部分转换为此格式。