VB.NET读取XML值

时间:2012-06-07 13:01:56

标签: xml vb.net

我一直试图为我的问题找到一个“优雅”的解决方案,但到目前为止,我无法按照我想要的方式工作。 我想做的是:

<profile id="Baseline">
    <package package-id="Software1" />
    <package package-id="Software2" />
    <package package-id="Software3" />

</profile>

<profile id="OLD_Profile">
    <depends profile-id='Baseline' />
    <package package-id="Software1" />
    <package package-id="Software2" />
    <package package-id="Software3" />
</profile>

这是我想要阅读的XML代码。 我想做的是。

阅读每个配置文件ID标签..然后为每个配置文件ID我想要阅读子标签..如“package package-id”和“depends profile-id”

我尝试了几种方法,但它们都很丑陋和错误。

感谢您的帮助

编辑:

想想我按照我想要的方式工作:

    Dim doc As New XmlDocument
    doc.Load("C:\profiles.xml")
    Dim hosts As XmlNodeList = doc.GetElementsByTagName("profile")

    For Each n As XmlElement In hosts
        For Each n2 As XmlElement In n
            Debug.Print(n2.GetAttribute("package-id").ToString())
            If Not n2.GetAttribute("profile-id").ToString() = Nothing Then
                Debug.Print(n2.GetAttribute("profile-id").ToString())
            End If
        Next

    Next

这似乎工作正常..我打赌有一个更优雅的解决方案,但它的工作原理:)

0 个答案:

没有答案
相关问题