getting XML attributes

johnfr

New member
Joined
May 19, 2007
Messages
2
Programming Experience
Beginner
hi,
i'm making a little VB program that has to read an XML file.
xml file:
HTML:
<Titles>
	<Title name="The Notebook" title="The Notebook" originaltitle="" year="2004" identifier="tt0332280" />
</Titles>

how can I get the value of for example name?

this is what I've got so far (but doens't work)
VB.NET:
'XmlDoc contains the XML file
Dim xpath As String = lstTitles.SelectedItem.ToString
Dim xmlnode As XmlNode = XmlDoc.SelectSingleNode("//Titles/Title[@name='" & xpath & "']")
txtTitle.text = xmlnode.Attributes.GetNamedItem("originaltitle").ToString
I always get the next value in the textbox: System.Xml.XmlAttribute

What am I doing wrong here?
 
Both Value and InnerText properties of that Attribute will return it. You can also short the expression: xmlnode.Attributes("originaltitle").InnerText
 
Both Value and InnerText properties of that Attribute will return it. You can also short the expression: xmlnode.Attributes("originaltitle").InnerText

thx :) it works now

but another question:
xmlnode = XmlDoc.SelectSingleNode("//Titles/Title[@name='" & xpath & "']")
if xpath contains a ' the program crashes (what is quit logical) because xpath in already between '
how can I ignore the ' in xpath?
 
change the select to doublequotes:
VB.NET:
Dim xmlnode As XmlNode = xmldoc.SelectSingleNode("//Titles/Title[@name=""" & xpath & """]")
 
Back
Top