Trouble with VB.NET and XPath
-
I was told that I may be able to get better help in this forum so I am reposting my question here. I'm trying to write a program that navigates an XML file with XPath but I'm having trouble if the XML file is structured a certain way. For example here is the code that navigates the XML file:
Dim XDoc as XPathDocument
Dim xmlNav as XPathNavigator
Dim xmlNI as XPathNodeIteratorXDoc = new XPathDocument("C:\sop.xml")
xmlNav = XDoc.CreateNavigator
xmlNI = xmlNav.Select("/SOP_FILE/QuestionList/Question")While xmlNI.MoveNext
MsgBox(xmlNI.Current.Name & " - " & xmlNI.Current.Value)
End WhileThis was just a test function I wrote to try out because this is my first time using XPath; hence it doesn't do much. Now, if I the XML is formated as follows, a message box is never displayed because the xmlNI count is zero:
<?xml version="1.0" encoding="utf-8"?>
<SOP_FILE xmlns="http://schemas.mycompany.com/SOP" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<QuestionList>
<Question Number="1">
<Text>Has a risk assessment been conducted?</Text><Choice Text="Yes" Index="1" PointsTo="2" />
</Question>
<Question Number="2">
<Text>Has an inspection been conducted?</Text><Choice Text="Yes" Index="1" PointsTo="3" />
<Choice Text="No" Index="2" PointsTo="4" />
</Question>
<Question Number="3">
<Text>Is the property lead-based paint free?</Text>
</Question>
</QuestionList>
</SOP_FILE>However if the second line of the XML file is changed to:
<SOP_FILE>
Instead of:
<SOP_FILE xmlns="http://schemas.mycompany.com/SOP" xmlns:xs="http://www.w3.org/2001/XMLSchema">
Then the test function works properly and loops through all the questions. Can someone please explain to me why this is happening and how to modify the test function so it will work properly. Thanks in advance for any help.
-
I was told that I may be able to get better help in this forum so I am reposting my question here. I'm trying to write a program that navigates an XML file with XPath but I'm having trouble if the XML file is structured a certain way. For example here is the code that navigates the XML file:
Dim XDoc as XPathDocument
Dim xmlNav as XPathNavigator
Dim xmlNI as XPathNodeIteratorXDoc = new XPathDocument("C:\sop.xml")
xmlNav = XDoc.CreateNavigator
xmlNI = xmlNav.Select("/SOP_FILE/QuestionList/Question")While xmlNI.MoveNext
MsgBox(xmlNI.Current.Name & " - " & xmlNI.Current.Value)
End WhileThis was just a test function I wrote to try out because this is my first time using XPath; hence it doesn't do much. Now, if I the XML is formated as follows, a message box is never displayed because the xmlNI count is zero:
<?xml version="1.0" encoding="utf-8"?>
<SOP_FILE xmlns="http://schemas.mycompany.com/SOP" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<QuestionList>
<Question Number="1">
<Text>Has a risk assessment been conducted?</Text><Choice Text="Yes" Index="1" PointsTo="2" />
</Question>
<Question Number="2">
<Text>Has an inspection been conducted?</Text><Choice Text="Yes" Index="1" PointsTo="3" />
<Choice Text="No" Index="2" PointsTo="4" />
</Question>
<Question Number="3">
<Text>Is the property lead-based paint free?</Text>
</Question>
</QuestionList>
</SOP_FILE>However if the second line of the XML file is changed to:
<SOP_FILE>
Instead of:
<SOP_FILE xmlns="http://schemas.mycompany.com/SOP" xmlns:xs="http://www.w3.org/2001/XMLSchema">
Then the test function works properly and loops through all the questions. Can someone please explain to me why this is happening and how to modify the test function so it will work properly. Thanks in advance for any help.
When you have a namespace in your XML document, you have to account for it in your XPath expression whether it is the default namespace or not. Look up "XmlNamespaceManager" in the System.Xml namespace.
"We make a living by what we get, we make a life by what we give." --Winston Churchill