Simple XPath Question
-
Hi, Having not found a good XPath tutorial out there yet, here is my question.. This is how I try to display the fisrtname of the first contact in the Contact.xml file, Consider this code:
XmlDocument myDoc = new XmlDocument(); myDoc.Load(Application.StartupPath + "\\Contact.xml"); txtPrénom.Text = myDoc.SelectSingleNode("//Prenom").Value;
What am I missing here? Thank you for your help! Antoine This by our hands that dream, "I shall find a way or make one!" -
Hi, Having not found a good XPath tutorial out there yet, here is my question.. This is how I try to display the fisrtname of the first contact in the Contact.xml file, Consider this code:
XmlDocument myDoc = new XmlDocument(); myDoc.Load(Application.StartupPath + "\\Contact.xml"); txtPrénom.Text = myDoc.SelectSingleNode("//Prenom").Value;
What am I missing here? Thank you for your help! Antoine This by our hands that dream, "I shall find a way or make one!"try txtPrénom.Text = myDoc.SelectSingleNode("//Prenom[first()]").Value; Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
-
Hi, Having not found a good XPath tutorial out there yet, here is my question.. This is how I try to display the fisrtname of the first contact in the Contact.xml file, Consider this code:
XmlDocument myDoc = new XmlDocument(); myDoc.Load(Application.StartupPath + "\\Contact.xml"); txtPrénom.Text = myDoc.SelectSingleNode("//Prenom").Value;
What am I missing here? Thank you for your help! Antoine This by our hands that dream, "I shall find a way or make one!"SelectSingleNode
returns first node matching XPath so your code should IMHO work, but only if "Prenom
" is element (not attribute). To select attrbute you need to use this XPath//@Prenom
. You can also use xpath like following:Xml:
<contacts>
<contact firstname="Bill">
...
<contact>
...
</contacts>XPath:
contacts/contact/@firstname
returns 'Bill'Great XPath tutorial is at www.w3schools.com[^].
Tomáš Petříček :baaaa!: EeekSoft Tools www.eeeksoft.net
-
SelectSingleNode
returns first node matching XPath so your code should IMHO work, but only if "Prenom
" is element (not attribute). To select attrbute you need to use this XPath//@Prenom
. You can also use xpath like following:Xml:
<contacts>
<contact firstname="Bill">
...
<contact>
...
</contacts>XPath:
contacts/contact/@firstname
returns 'Bill'Great XPath tutorial is at www.w3schools.com[^].
Tomáš Petříček :baaaa!: EeekSoft Tools www.eeeksoft.net
-
Hi, Having not found a good XPath tutorial out there yet, here is my question.. This is how I try to display the fisrtname of the first contact in the Contact.xml file, Consider this code:
XmlDocument myDoc = new XmlDocument(); myDoc.Load(Application.StartupPath + "\\Contact.xml"); txtPrénom.Text = myDoc.SelectSingleNode("//Prenom").Value;
What am I missing here? Thank you for your help! Antoine This by our hands that dream, "I shall find a way or make one!"You have the right idea, but it depends on your actual XML. I suggest getting the node directly and looking at it. This will help answer your question. DogCat XmlNode node = doc.SelectSingleNode("//Prenom"); node.Value == null node.InnerText == "Dog"