views:

50

answers:

2

In my XML, it's possible for an apostrophe to appear in a node's value:

<Root>
    <Sections>
        <SectionA>
            <Heading>Title</Heading>
            <Description>This is section 'A'</Description>
        </SectionA>
    </Sections>
</Root>

If I have controls bound to this XML:

<asp:FormView ID="myFormView" runat="server" DataSourceID="myXmlDataSource">
    <ItemTemplate>
        <div>
            HTML Element:
            <input type="text" value='<%# XPath("text()") %>' />
        </div>
        <div>    
            Server Control:
            <asp:TextBox id="myTextBox" runat="server" value='<%# XPath("text()") %>' />
        </div>
    </ItemTemplate>
</asp:FormView>           
<asp:XmlDataSource ID="myXmlDataSource" runat="server" XPath="Root/Sections/SectionA" />

I've noticed that the text is correctly displayed in the asp:TextBox but not in the INPUT element. I'm assuming that it's because server controls correctly escape the apostrophe. To work around this, I tried changing the Description node in the XML to the following:

<Description>This is section &#39;A&#39;</Description>

Again, this displayed correctly in the asp:TextBox, but not in the INPUT element.

My next attempt was to wrap the node's value in a CDATA:

<Description><![CDATA[This is section &#39;A&#39;]]></Description>

Finally it was displaying correctly in the INPUT element, but now the asp:TextBox displayed the two "& # 3 9 ;". I've even tried "& a p o s ;" but the result is the same.

What is the best approach for the use of apostrophes in XML where the value can be displayed in either a server control or HTML element?

A: 

Use &apos; instead. It is one of the 5 permitted entities in XML.

So your code would look like:

<Description>This is section &apos;A&apos;</Description>
Robusto
I've tried ' as well, but the result is the same as '
Bullines
@Bulines This may be an encoding issue. Try ' (put ) and see if that helps.
Robusto
When not in a CDATA, it works fine in the INPUT, but displays "'" in the asp:TextBox.
Bullines
+1  A: 

Here you have single quotes surrounding the value argument for the html element:

 <input type="text" value='<%# XPath("text()") %>' />

This renders:

value='This is section 'A'' 

Instead use double quotes:

 <input type="text" value="<%# XPath("text()") %>" />

Which renders:

<input type="text" value="This is section 'A'" />
thedugas
I'm not sure why I thought that single quotes were needed for the INPUT element :)
Bullines