Last post Aug 05, 2014 03:08 PM by philos
Aug 05, 2014 02:00 AM|philos|LINK
i have been trying a number of things to resolve this apostrophe (single quote) problem. i have an attribute in a node that uses an apostrophe that i need to use in the XPath of an XmlDataSource, but have been unable to find the correct sequence of escape
characters to include it in the final string.
in the aspx:
<asp:XmlDataSource ID="XmlDataSource1" runat="server"></asp:XmlDataSource>
the codebehind builds the xpath string:
Dim xstr As String = ""
xstr = "/books/book[@title='Tom"
xstr = xstr & "'"
xstr = xstr & "s Cafe']"
Dim xdatasrc As XmlDataSource = XmlDataSource1
xdatasrc.XPath = xstr
xdatasrc.DataFile = "~/App_Data/books.xml"
i am trying to get to an XPath that finds a node with the attribute, Tom's Cafe. For example: XPath="/books/book[@title='Tom's Cafe']"
<?xml version="1.0" encoding="utf-8" ?>
<book title="Tom's Cafe">
text text text text
i end up with a blank result or an error that says that there is an invalid token.
would anyone have any suggestions on how to build this xpath that includes an apostrophe in one of the attributes?
Aug 05, 2014 04:35 AM|MetalAsp.Net|LINK
My only suggestion is this:
Remove this line: Dim xdatasrc
And use this:
XmlDataSource1.XPath = xstr
XmlDataSource1.DataFile = "~/App_Data/books.xml"
I can't see anything wrong with your selector. Though, I think, you can write it in just one line: xstr = "/books/book[@title='Tom's Cafe']"
Aug 05, 2014 05:57 AM|philos|LINK
thanks for the more concise code, i modified as suggested. i then tried the XPath built string and again as a single string on one line. unfortunately, the result was blank and did not display the text under the node.
in locals, the XPath was equal to: "/books/book[@title='Tom's Cafe']"
in the aspx page, i am simply displaying the text in a paragraph.
just to verify that the problem is with the apostrophe, i removed it from the xml file and used: "/books/book[@title='Toms Cafe']" as the XPath string. the page ran without error and the text was displayed on the web page.
Aug 05, 2014 06:08 AM|MetalAsp.Net|LINK
I just saw this:
Apparently they double the single quote. Not a VB person, so this is not tested, nor verified.
Aug 05, 2014 03:08 PM|philos|LINK
well, i tried the first option listed in this article and received the now typical token error:
'/books/book[@title='Tom''s Cafe']' has an invalid token.
but, when i tried the second option listed in the article, success! i needed to replace the single quotes around the attribute with double quotes. probably the only option i haven't tried.
this works in vb:
'XmlDataSource1.XPath = "/books/book[@title=""Tom's Cafe""]"
'XmlDataSource1.DataFile = "~/App_Data/books.xml"
thanks so much!