2010-11-24 74 views
0

當我試圖在Sharepoint中打開一個頁面時出現上述錯誤。實際上我用下面的代碼:XML解析錯誤:不正確

protected void Page_Load(object sender, EventArgs e) 
{ 
    Response.ContentType = "text/xml"; 
    Response.Write(GenerateTagCloud()); 
    Response.End();  
} 

private string GenerateTagCloud() 
{ 
    SPWeb myweb = null; 
    SPSecurity.RunWithElevatedPrivileges(delegate() 
    { 
     SPSite mysite1 = new SPSite(mysite.ID); 
     myweb = mysite1.OpenWeb(); 
    }); 

    myweb.AllowUnsafeUpdates = true; 

    SPList categories = myweb.Lists["Discussion Categories"]; 
    SPList discussion = myweb.Lists["Team Discussion"]; 

    System.Text.StringBuilder sp = new System.Text.StringBuilder(); 
    //System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); 

    sp.Append("<?xml version='1.0' encoding='UTF-8'?>"); 
    sp.Append("<tags>"); 

    foreach (SPListItem category in categories.Items) 
    {    
     string categoryname = category.Name; 
     categoryname = categoryname.Replace(" ", "%20"); 
     sp.Append("<a href='" + myweb.Url + "/Pages/Home.aspx?Discussion=" + categoryname + "' style='font-size: 13.5pt' color='0x7b7b7b'>" + category.Name + "</a>"); 
    } 
    sp.Append("</tags>"); 

    myweb.AllowUnsafeUpdates = false; 
    return sp.ToString(); 

} 

但在輸出它顯示爲:在& lt和& GT標籤節點的標籤打開和關閉。如何糾正這一點?

+0

sp.Append(「);有沒有在上面的代碼中,它沒有顯示在上面的代碼 – Abhimanyu 2010-11-24 13:25:37

回答

1

由於這個問題是在類別名稱中的一個&,所有你需要做的就是逃避它 - 有五個字符需要在XML中進行轉義:

  • & - &amp;
  • < - &lt;
    • &gt;
  • 「 - &quot;
  • ' - &apos;

&的情況下,只需用&amp;替換它。

話雖如此,如果你正在編寫XML你應該用一個XmlWriter而不是StringBuilder - 這將正確地逃脫字符,不會發生這種錯誤的:從here採取

XmlWriter writer = XmlWriter.Create(Console.Out); 
writer.WriteStartElement("Foo"); 
writer.WriteAttributeString("Bar", "Some & value"); 
writer.WriteElementString("Nested", "data & data"); 
writer.WriteEndElement(); 

代碼。