apdant Posted February 2, 2004 Posted February 2, 2004 Hello all, I have been lurking for a few months around the forums and coding quietly in a dark corner, but recently I have hit a problem I just can't seem to solve. I am writing an application in VB.NET, and attempting to read in configuration information from a .XML file. I read through the MSDN samples and it seemed extremely straightforward, but when I tried to make it work I ran into problems. I took a small code sample from these forums to read in the value. (slightly modified) ---------------------------------------------------------------- Function GetValue(ByVal key as String) As String Try Dim xmlDoc As New XmlDocument() xmlDoc.Load("C:\test.xml") Return xmlDoc.SelectSingleNode("//" & key).InnerText Catch ex As Exception("Error reading file: test.xml", ex) End Try End Function ---------------------------------------------------------------- The first time I tried it, the code returned a value, and everything seemed to work fine. I came back into work the next day, and as things often go... I changed something and it refuses to work now. I have tried using the MSDN samples for the .XML file to make sure my formatting on the file is correct, I have tried using SelectSingleNode, and SelectNodes, wrote different code several times, but both methods return a null value. I can assign a variable to the root and access the file: ----------------------------------------------------------------- Dim doc As XmlDocument = New XmlDocument() doc.Load("C:\test.xml") Dim root As XmlNode = doc.DocumentElement msgbox(root.FirstChild.InnerText) ----------------------------------------------------------------- This tells me that I have accessed the .XML file, I can see the data inside it, and I can manuever the values. When I try to select a specific node using SelectSingleNode/SelectNodes however, I get a null value regardless of what I put in the XPath. I have tried everything I can think of at this point... and I'm basically out of ideas. I appreciate any ideas you have! Aaron Quote
samsmithnz Posted February 2, 2004 Posted February 2, 2004 Try using: msgbox(root.FirstChild.OuterXML) to make sure you are exactly where you think you are... Quote Thanks Sam http://www.samsmith.co.nz
apdant Posted February 2, 2004 Author Posted February 2, 2004 I did that and got header information (<author>Aaron P. Dant</author> etc) ... I am sure that I have access to the .XML file, I just can't seem to make it return a specific node. Quote
*Experts* Bucky Posted February 2, 2004 *Experts* Posted February 2, 2004 Perhaps I'm unfamiliar with the syntax, but that is the oddest- looking error handling I've seen... See if you get any better results with this: Function GetValue(ByVal key as String) As String Try Dim xmlDoc As New XmlDocument() xmlDoc.Load("C:\test.xml") Return xmlDoc.SelectSingleNode("//" & key).InnerText Catch MessageBox.Show("Error reading file: test.xml") Return Nothing ' Return nothing if an error occurs End Try End Function Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
samsmithnz Posted February 2, 2004 Posted February 2, 2004 So using InnerText should get you "Aaron P. Dant" right? Quote Thanks Sam http://www.samsmith.co.nz
samsmithnz Posted February 2, 2004 Posted February 2, 2004 Actually I usually use this: Try 'code to try here Catch ex as exception MessageBox.Show("Error reading file: test.xml: " & ex.tostring) End Try Quote Thanks Sam http://www.samsmith.co.nz
apdant Posted February 2, 2004 Author Posted February 2, 2004 Bucky, you are right the error handling was very odd, but as I was trying something from an example I had left it virtually untouched on my first attempt. The first attempt worked, so I recoded it from scratch... but it did not return a value. I reverted to the "odd" sample code and no longer get anything from that now! I tried your snippet and the node is still not found. SelectSingleNode does not seem inclined to actually return the node, so it sets the object to "Nothing". Quote
samsmithnz Posted February 2, 2004 Posted February 2, 2004 That is strange... returning <author>Aaron P. Dant</author> when using the outerXML seems to indicate that itsn't nothing.... Quote Thanks Sam http://www.samsmith.co.nz
*Experts* Bucky Posted February 2, 2004 *Experts* Posted February 2, 2004 Are you sure that the element in the file actually has some text in it? SelectSingleNode would return Nothing if the node didn't exist, and then an error would be thrown when trying to access InnerText. Therefore the node must exist, but it contains no actual text. Try InnerXml and see where that gets you. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
apdant Posted February 2, 2004 Author Posted February 2, 2004 It only returns information if I use doc.DocumentElement to set the root. Then I can get some values from FirstChild & LastChild, but it will not let me select an individual node or list of nodes. So I can select the root value, which shows that I have access to the file... but I cannot select a specific node or value. Quote
apdant Posted February 2, 2004 Author Posted February 2, 2004 That is exactly what is happening Bucky... it returns "Nothing" to the XmlNode object, and the .InnerText property errors the program out. This is assuming that I am trying to use SelectSingleNode/SelectNodes. Quote
*Experts* Bucky Posted February 3, 2004 *Experts* Posted February 3, 2004 The XPath expression must not be correct then. Post an example of what your XML file looks like and give an example of what key would be and what it would return. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
apdant Posted February 3, 2004 Author Posted February 3, 2004 I have been using the sample XML file from the MSDN help, to test. I have tried everything I can think of, here are a couple examples I have tried: dim doc as XmlDocument = New XmlDocument() doc.Load("C:\test.xml") dim root as XmlNode = doc.DocumentElement root.SelectSingleNode("descendant::book[author/last-name='Austen']") root.SelectSingleNode("//book") Here is the XML code. <?xml version="1.0"?> <!-- A fragment of a book store inventory database --> <bookstore xmlns:bk="urn:samples"> <book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8"> <title>Pride And Prejudice</title> <author> <first-name>Jane</first-name> <last-name>Austen</last-name> </author> <price>24.95</price> </book> <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1"> <title>The Handmaid's Tale</title> <author> <first-name>Margaret</first-name> <last-name>Atwood</last-name> </author> <price>29.95</price> </book> <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6"> <title>Emma</title> <author> <first-name>Jane</first-name> <last-name>Austen</last-name> </author> <price>19.95</price> </book> <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3"> <title>Sense and Sensibility</title> <author> <first-name>Jane</first-name> <last-name>Austen</last-name> </author> <price>19.95</price> </book> </bookstore> Quote
apdant Posted February 3, 2004 Author Posted February 3, 2004 Still no luck, but I have been playing with XmlTextReader and manually parsing the values. It's messy, but I cannot seem to get SelectSingleNode to work. Quote
*Experts* Bucky Posted February 3, 2004 *Experts* Posted February 3, 2004 I should have linked to this before, but W3Schools is a great resource for learning XPath, XML, XSLT, and all that good stuff. Check it out first if you're stuck in the future. Now this is untested, but it should work: ' Selects the first book node with the author's last name of Austen Dim bookNode As XmlNode = root.SelectSingleNode("//book[author/last-name='Austen']") ' Show the title MessageBox.Show(bookNode.Item("title").InnerText) Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
apdant Posted February 3, 2004 Author Posted February 3, 2004 I seem to have solved the problem which was actually not related to the code at all... the XML document was being saved off in Word 2003, which added a ton of formatting to it. This formatting for some reason totally ruined the program. I rewrote the XML document and saved it in plain text and the program ran fine. Thanks for the help guys. :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.