在服务器端XML处理时出错

时间:2012-01-26 02:38:57

标签: c# asp.net xml xslt

所以我有一个aspx页面,它将XML + XSL服务器连接到客户端,并进行客户端转换,该工作正常。

我正在尝试检测客户端,如果他们不支持客户端转换,我正在服务器端。我正在中断渲染处理器将返回XML的aspx页面,我得到它的输出,将它与XSL页面的输出结合起来并提供它。然而,该输出没有很好地形成。我得到了

XML Parsing Error: mismatched tag. Expected: </link>.
Location: http://oohrl.com/dashboard.aspx
Line Number 36, Column 20:        </script></head>
-------------------^

在客户端生成的输出中,工作正常,我得到了例如

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <link rel="stylesheet" type="text/css" href="./css/dboard.css"/>
    <link rel="stylesheet" type="text/css" href="./css/dboardmenu.css"/>
    <script type="text/javascript" src="./js/simpletabs.js"/>
    <link href="../css/simpletabs.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"/>
    <script type="text/javascript">
                $(document).ready(function () {
                    $("#BlogSelectList li a").live("click", function () {
                        var str = ($(this).attr("href")).slice(1, 37)
                        $.ajax({
                            contentType: "application/json; charset=utf-8",
                            url: '../ws/WebServices.asmx/SetActiveBlog',
                            data: '{ActiveBlogID: "' + str + '"}',
                            dataType: 'json',
                            type: "post",
                            success: function (j) {
                                    window.location.href = 'dashboard.aspx'
                            }

                        });

                    });
                })

        function showlayer(layer) {
            var myLayer = document.getElementById(layer);
            if (myLayer.style.display == "none" || myLayer.style.display == "") {
                myLayer.style.display = "block";
                }
                else {
            myLayer.style.display = "none";
            }
        }

        </script></head>

如果我生成服务器端,我会

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
      <head>
        <META http-equiv="Content-Type" content="text/html; charset=utf-16">
        <link rel="stylesheet" type="text/css" href="./css/dboard.css">
        <link rel="stylesheet" type="text/css" href="./css/dboardmenu.css">
<script type="text/javascript" src="./js/simpletabs.js"></script>
<link href="../css/simpletabs.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js">
</script>
<script type="text/javascript">
                    $(document).ready(function () {
                        $("#BlogSelectList li a").live("click", function () {
                            var str = ($(this).attr("href")).slice(1, 37)
                            $.ajax({
                                contentType: "application/json; charset=utf-8",
                                url: '../ws/WebServices.asmx/SetActiveBlog',
                                data: '{ActiveBlogID: "' + str + '"}',
                                dataType: 'json',
                                type: "post",
                                success: function (j) {
                                        window.location.href = 'dashboard.aspx'
                                }

                            });

                        });
                    })

                        function showlayer(layer) {
                            var myLayer = document.getElementById(layer);
                            if (myLayer.style.display == "none" || myLayer.style.display == "") {
                                myLayer.style.display = "block";
                            }
                            else {
                                myLayer.style.display = "none";
                            }
                        }


        </script></head>

这给了我错误。当然我注意到<link/> vs <link>标签的区别,但我不知道为什么服务器端处理引擎会给我不同的结果或如何修复它?

以下是我用于在服务器上生成XHTML的代码

    protected override void Render(HtmlTextWriter writer)
    {

        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
        HtmlTextWriter hWriter = new HtmlTextWriter(sw);
        base.Render(hWriter);
        // *** store to a string
        string XMLOutput = sb.ToString();
        // *** Write it back to the server
        if (!Request.Browser.IsBrowser("IE"))
        {
            writer.Write(XMLOutput);
        }
        else
        {
            StringWriter XSLsw = new StringWriter();
            HttpContext.Current.Server.Execute("DashboardXSL.aspx", XSLsw);
            string output = String.Empty;
            using (StringReader srt = new StringReader(XSLsw.ToString())) // xslInput is a string that contains xsl
            using (StringReader sri = new StringReader(XMLOutput)) // xmlInput is a string that contains xml
            {
                using (XmlReader xrt = XmlReader.Create(srt))
                using (XmlReader xri = XmlReader.Create(sri))
                {
                    XslCompiledTransform xslt = new XslCompiledTransform();
                    xslt.Load(xrt);
                    using (StringWriter _sw = new StringWriter())
                    using (XmlWriter xwo = XmlWriter.Create(_sw, xslt.OutputSettings)) // use OutputSettings of xsl, so it can be output as HTML
                    {
                        xslt.Transform(xri, xwo);
                        output = _sw.ToString();
                    }
                }
            }
            writer.Write(output);
            Response.Flush();
            Response.End();
       }

2 个答案:

答案 0 :(得分:1)

由于输出文档的根元素是<html>,因此处理器选择HTML作为默认格式。要改为创建格式良好的XHTML文档,请确保您的XSLT包含以下作为根<xsl:stylesheet><xsl:transform>元素的子项:

<xsl:output method="xml" omit-xml-declaration="yes" />

答案 1 :(得分:0)

我必须将xsl表上的内容类型设置为text / html,以修复所有问题。

请注意,此更改仅在转换服务器端时使用。将其发送到客户端进行客户端转换时,不会更改为text / html