如何从XML DOM中获取属性名称

时间:2011-07-20 16:31:55

标签: asp.net xml vb.net

我从xml文档中获取属性名称的问题。这是片段:

<RUNWAY name="02X" length="3507" slope="-0.11" level_off_height="2228">

我创建了一个循环来显示结果。在上面的代码段中,我如何获取属性名称(名称,长度,斜率和level_of_height)。我用过reader.name给了我RUNWAY,这是正确的元素名称。我尝试了getAttribute,它给了我所有属性的值,但没有名字。 请原谅(!),因为它们是打开和关闭的括号。它不会让我在那里发布我的问题。任何帮助将非常感谢

1 个答案:

答案 0 :(得分:0)

如果您使用的是XmlReader,请尝试Reading Attributes (from MSDN)

' Display all attributes.
If reader.HasAttributes Then
  Console.WriteLine("Attributes of <" + reader.Name + ">")
  Dim i As Integer
  For i = 0 To (reader.AttributeCount - 1)
    Console.WriteLine("  {0}", reader(i))
  Next i
  ' Move the reader back to the element node.
  reader.MoveToElement() 
End If

或其他方式:

If reader.HasAttributes Then
  Console.WriteLine("Attributes of <" + reader.Name + ">")
  While reader.MoveToNextAttribute()
    Console.WriteLine(" {0}={1}", reader.Name, reader.Value)
  End While
  ' Move the reader back to the element node.
  reader.MoveToElement()
End If
相关问题