VB.NET获取XML中父节点的属性

时间:2015-11-27 16:37:19

标签: xml vb.net

让我们说我正在迭代以下XML:

<Account Info="1234">
  <Number>3333</Number>
</Account>
<Account Info="0007">
  <Number>4444</Number>
</Account>

Dim MyNodes As XmlNodeList = xmldoc.GetElementsByTagName("Account")

For Each node As XmlNode In MyNodes
    'Get attribute of the current 'Info' node
Next

我需要以某种方式返回循环内并获取父节点的属性值。

帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

XmlNode有一个ParentNode属性

示例

Dim nodes As XmlNodeList = xmldoc.GetElementsByTagName("Account")
For Each node As XmlNode In nodes
    Dim parentNode As XmlNode = node.ParentNode
    If parentNode Is Nothing OrElse parentNode.Attributes Is Nothing Then
        Continue For
    End If
    Dim attribute As XmlAttribute = parentNode.Attributes.ItemOf("SomeAttribute")
    If attribute Is Nothing Then
        Continue For
    End If
    Console.WriteLine(attribute.Value)
Next