代码之家  ›  专栏  ›  技术社区  ›  Bullines

XML和数据绑定控件中的撇号

  •  1
  • Bullines  · 技术社区  · 14 年前

    在我的XML中,一个撇号可能出现在节点的值中:

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

    如果我有绑定到此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" />
    

    我注意到文本正确地显示在asp:textbox中,而不是输入元素中。我假设这是因为服务器控件正确地避开了撇号。为了解决这个问题,我尝试将xml中的description节点更改为以下内容:

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

    同样,这在asp:textbox中正确显示,但在input元素中不正确。

    我的下一个尝试是将节点的值包装到CData中:

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

    最后,它在input元素中正确显示,但现在asp:textbox显示了两个“&39;”。我甚至试过“A P O S”,但结果是一样的。

    在XML中使用撇号的最佳方法是什么?在XML中,值可以显示在服务器控件或HTML元素中?

    2 回复  |  直到 14 年前
        1
  •  1
  •   dugas    14 年前

    在这里,HTML元素的值参数周围有单引号:

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

    这使得:

    value='This is section 'A'' 
    

    相反,请使用双引号:

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

    使:

    <input type="text" value="This is section 'A'" />
    
        2
  •  0
  •   Robusto    14 年前

    请改用'。它是XML中允许的5个实体之一。

    所以你的代码看起来像:

    <Description>This is section &apos;A&apos;</Description>