2009-10-22 77 views
2

我該如何解決在xml元素中嵌入html錨點的問題?考慮以下xml:在XML文檔中嵌入HTML錨點

<?xml version="1.0" encoding="utf-8"?> 
<Customers> 
    <Customer> 
      <FirstName>Joe</FirstName> 
      <LastName>Mama</LastName> 
      <Email><a href="mailto:[email protected]">[email protected]</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代碼隱藏

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

XML是XML ,HTML是HTML。如果您試圖將XML解析爲HTML,然後提取<Email>的內容,我建議您使用<![CDATA[]]>進行包裝,並將其吐出爲HTML格式。

也請提供您用於顯示此代碼。

2

以下XML完美適用於上述示例。

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