Estrarre attributi dal nodo

martedì 28 dicembre 2004 - 16.22

babbalao Profilo | Newbie


Devo tirarmi fuori gli attributi del nodo "golfer" descritto nel file in allegato.
Il ciclo sottostante mi va in errore.
("Object reference not set to an instance of an object" sulla riga
objdr("Skill") = objnode("golfer").GetAttribute("skill") )


Dim objdr As DataRow
Dim row As System.Xml.XmlNodeList = xmldoc.GetElementsByTagName
("golfer")
Dim objnode As System.Xml.XmlNode

For Each objnode In row
objdr = objTable.NewRow()
objdr("Skill") = objnode("golfer").GetAttribute("skill")
objdr("Handicap") = objnode("golfer").GetAttribute("handicap")
objdr("Clubs") = objnode("golfer").GetAttribute("clubs")
objdr("Id") = objnode("golfer").GetAttribute("id")
objdr("First Name") = objnode("firstName").InnerText
objdr("Last Name") = objnode("lastName").InnerText
objTable.Rows.Add(objdr)
Next

Qulcuno sa aiutarmi?

Earendil81 Profilo | Newbie

Forse ho trovato quello che cercavi(serviva anche a me e lo condivido)

Imports System.IO
Imports System.Xml
Module ParsingUsingXmlDocument
Sub Main()
Try
Dim m_xmld As XmlDocument
Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode
'Create the XML Document
m_xmld = New XmlDocument()
'Load the Xml file
m_xmld.Load("C:\CMS\Personal\family.xml")
'Get the list of name nodes
m_nodelist = m_xmld.SelectNodes("/family/name")
' Questa riga è fondamentale la devi utilizzare per posizionarti nel nodo corretto da cui vuoi estrarre l'attributo(in questo caso GENDER)

'Loop through the nodes
For Each m_node In m_nodelist
'Get the Gender Attribute Value
Dim genderAttribute = m_node.Attributes.GetNamedItem("gender").Value
'Get the firstName Element Value
Dim firstNameValue = m_node.ChildNodes.Item(0).InnerText
'Get the lastName Element Value
Dim lastNameValue = m_node.ChildNodes.Item(1).InnerText
'Write Result to the Console
Console.Write("Gender: " & genderAttribute _
& " FirstName: " & firstNameValue & " LastName: " _
& lastNameValue)
Console.Write(vbCrLf)
Next
Catch errorVariable As Exception
'Error trapping
Console.Write(errorVariable.ToString())
End Try
End Sub
End Module

Bye ^_^

babbalao Profilo | Newbie

Alla fine ho risolto così..ciclando sui nodi, cmqe grazie ;-) ciao

nodo = "golfers"
row = xmldoc.GetElementsByTagName(nodo)
For Each objnode In row
For Each objnode2 In objnode.ChildNodes
objdr = objTable.NewRow()
objdr("Skill") = objnode("golfer").GetAttribute("skill")
objdr("Handicap") = objnode("golfer").GetAttribute("handicap")
objdr("Clubs") = objnode("golfer").GetAttribute("clubs")
objdr("Id") = objnode("golfer").GetAttribute("id")
If objnode2.HasChildNodes Then
For Each subNode In objnode2.ChildNodes
If subNode.HasChildNodes Then
For Each subNode2 In subNode
If subNode.Name = "name" Then
objdr("First Name") = subNode("firstName").InnerText
objdr("Last Name") = subNode("lastName").InnerText
End If
Next
End If
Next
End If
objTable.Rows.Add(objdr)
Next
Next

babbalao Profilo | Newbie

Alla fine ho risolto così (nel file allegato puoi vederlo)..ciclando sui nodi, cmqe grazie ;-) ciao e alla prossima.

Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5