XML 解析错误:格式不正确

发布于 2024-10-04 08:35:21 字数 1348 浏览 6 评论 0原文

当我尝试在共享点中打开页面时,出现上述错误。实际上我使用了以下代码:

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 中标签节点的标签打开和关闭。如何纠正这个问题?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静水深流 2024-10-11 08:35:21

由于问题在于类别名称之一中的 &,因此您所需要做的就是转义它 - XML 中需要转义五个字符:

  • & - &
  • < - <
    • >

  • " - "
  • ' - '

如果是 & ,只需将其替换为 &

话虽如此,如果您正在编写 XML,则应该使用 XmlWriter 而不是 StringBuilder。 - 这将正确转义字符,并且不会发生此类错误:

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

代码取自 此处

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 a StringBuilder - this will escape the characters correctly and this kind of error will not occur:

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

Code taken from here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文