在XML文档中嵌入HTML锚点

时间:2009-10-22 00:45:47

标签: html xml

我如何在xml元素中嵌入html锚点?请考虑以下xml:

<?xml version="1.0" encoding="utf-8"?>
<Customers>
     <Customer>
          <FirstName>Joe</FirstName>
          <LastName>Mama</LastName>
          <Email><a href="mailto:joemama@gmail.com">joemama@gmail.com</a></Email>
          <Website><a href="http://www.joemama.com">www.joemama.com</a></Website>
     </Customer>
</Customer>

当我显示元素内容时,我只获得文本,没有超链接。

以下是我用于在asp.net Web表单页面中显示xml数据的代码:

CustomerView HTML

<%@ control language="vb" autoeventwireup="false" codebehind="CustomerView.ascx.vb"
    inherits="Sparta.Web.CustomerView" %>
<div class="View">
    <table>
         <tr>
             <td>First Name:</td>
             <td><asp:label id="FirstName" runat="server"></asp:label></td>
         </tr>
         <tr>
             <td>Last Name:</td>
             <td><asp:label id="LastName" runat="server"></asp:label></td>
         </tr>
         <tr>
             <td>Email:</td>
             <td><asp:literal id="Email" runat="server"></asp:literal></td>
         </tr>
         <tr>
             <td>Website:</td>
             <td><asp:literal id="Website" runat="server"></asp:literal></td>
         </tr>
    </table>
</div>

CustomerView Codebehind

Public Partial Class CustomerView
    Inherits System.Web.UI.UserControl

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
          Me.DataBind()
     End Sub

     Private Sub DataBind()

        Dim xmlDoc As System.Xml.Linq.XDocument = Nothing
        xmlDoc = XDocument.Load(Server.MapPath("~/data/CustomerData.xml"))

        Dim listQuery = _
        From list In xmlDoc.Descendants("Customer") _
        Where list.Element("LastName").Value = Request.QueryString("id") _
        Select _
        FirstName = list.Element("FirstName").Value, _
        LastName= list.Element("LastName").Value, _
        Email = list.Element("Email").Value, _
        Website = list.Element("Website").Value

        Dim listInfo = listQuery(0)
        If listInfo Is Nothing Then
            Throw New ApplicationException("Missing CustomerData Element")
        End If

        Me.FirstName.Text = listInfo.FirstName
        Me.LastName.Text = listInfo.LastName
        Me.Email.Text = listInfo.Email
        Me.Website.Text = listInfo.Website

     End Sub

End Class

2 个答案:

答案 0 :(得分:2)

XML是XML,HTML是HTML。如果您尝试将XML解析为HTML,则提取<Email>的内容,我建议使用<![CDATA[]]>进行包装,然后将其吐出HTML格式。

另请提供您用来显示此代码的代码。

答案 1 :(得分:2)

以下XML适用于上面的示例。

<?xml version="1.0" encoding="utf-8"?>
<Customers>
     <Customer>
          <FirstName>Joe</FirstName>
          <LastName>Mama</LastName>
          <Email><![CDATA[<a href="mailto:joemama@gmail.com">joemama@gmail.com</a>]]></Email>
          <Website><![CDATA[<a href="http://www.joemama.com">www.joemama.com</a>]]></Website>
     </Customer>
</Customer>