How to find contents of an XML Node that starts with an apostrophe?
I have some code that I am using to try to learn XML parsing in VB.NET.
(Not even really sure if this is the way to go; just wanted to see if my
understanding of what I was currently reading was accurate, so bear with
me on the beginner level programming).
XMLQuotes = New XmlDocument
XMLQuotes.Load("XMLDocs/IKAbx.xml")
Dim nodAuthor As XmlElement = XMLQuotes.DocumentElement
Dim nodItems As XmlNodeList = nodAuthor.SelectNodes("/Authors/Author")
For i = 0 To nodItems.Count - 1
'grab info from the XML using the supplied node
authorName = GetNodeValue("Name", doc)
'etc.
'try to get a list of quotes from the author.
'the next line is the line that does not work.
Dim nodQuotes As XmlNodeList =
nodReqs.SelectNodes("/Authors/Author[Name='" &
Replace(nodItems(i).Item("Name").InnerXml, "'", "'") &
"']/Quote")
For j = 0 To nodQuotes.Count - 1
quotes.Add nodBonSk(j).InnerXml
Next
'continue processing
Next I
This process works great, with one exception. If I have a quote that
starts with an apostrophe (such as "'Tis nobler in the hearts..." [and
yes, I do know that that quote starts with "Whether", but this is to
illustrate the point at hand]), then it does not actually find the node
with the appropriate Name element. It returns a count of 0 nodes and moves
on.
I have tried using the &apos, ' escape characters in the XML file (an d
even tried just a pure single quote), but nothing seems to work. So, what
am I doing wrong?
Private Function GetNodeValue(nodeName As String, doc As String) As Object
Dim doc2 As XDocument = XDocument.Parse(doc)
Dim element = doc2.Root.Element(nodeName)
If IsNothing(element) Then
Return Nothing
Else
Return element.Value
End If
End Function
No comments:
Post a Comment