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();
}
但在输出中它显示为: < 和 > 中标签节点的标签打开和关闭。如何纠正这个问题?
when i am trying to open a page in sharepoint the above error is appearing. actually i used the following code :
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();
}
but in the output it shows as : tag opening and closing of the tags node in < and > . how to correct this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于问题在于类别名称之一中的
&
,因此您所需要做的就是转义它 - XML 中需要转义五个字符:&
<
"
'
如果是
&
,只需将其替换为&
话虽如此,如果您正在编写 XML,则应该使用
XmlWriter
而不是StringBuilder
。 - 这将正确转义字符,并且不会发生此类错误:代码取自 此处。
Since the issue is with a
&
in one of the category names, all you need to do is escape it - there are five characters that need to be escaped in XML:&
<
"
'
In the case of
&
, simply replace it with&
.Having said that, if you are writing XML you should be using an
XmlWriter
and not aStringBuilder
- this will escape the characters correctly and this kind of error will not occur:Code taken from here.