XML StringWriter 空

发布于 2024-12-06 18:32:12 字数 1969 浏览 2 评论 0原文

我在 XMLDocument 中有一些 XML,我想将其放入 StringWriter 中,但它总是变成空(?)。我尝试过使用调试检查变量,但我仍然不知道发生了什么。

这是我所拥有的:

        XmlDocument xmlInput = new XmlDocument();
        /* after this executes, xmlInput has lots of data in it */  
        xmlInput.LoadXml(...); 

        XslCompiledTransform transform = new XslCompiledTransform();
        /* I'm pretty sure Template.xslt is being found and loaded correctly */
        transform.Load(Server.MapPath("/Files/Template.xslt")); 

        using (System.IO.StringWriter sb = new System.IO.StringWriter())
        {
            XmlWriterSettings xSettings = new XmlWriterSettings();
            xSettings.ConformanceLevel = ConformanceLevel.Fragment;
            xSettings.Encoding = Encoding.UTF8;
            /* PROBLEM: After this line, StringWriter sb is {} */
            using (XmlWriter xWriter = XmlWriter.Create(sb, xSettings))
            {
                transform.Transform(xmlInput, xWriter);
            }

            /* xmlText is empty string "" after this line */
            String xmlText = sb.ToString();

            /* ... does more stuff ... */
        }

我可以帮忙吗?我真的不知道从这里到哪里去让它工作。

编辑:更多的调查:

        using (var stringWriter = new StringWriter())
        {
            using (var xmlTextWriter = XmlWriter.Create(stringWriter))
            {
                /* Prints lots of data to the screen */
                Response.Write(xmlInput.InnerXml);

                xmlInput.WriteTo(xmlTextWriter);
                /* Doesn't print anything to the screen */
                Response.Write(stringWriter.GetStringBuilder().ToString() );
            }
        }

但是,冲洗就可以了

                xmlInput.WriteTo(xmlTextWriter);
                xmlTextWriter.Flush();
                /* prints lots of data */
                Response.Write( stringWriter.GetStringBuilder().ToString() );

I have some XML in a XMLDocument and I want to put it into a StringWriter, but it always becomes empty(?) instead. I've tried inspecting the variables with debug, but I'm still clueless as to what's going on.

Here's what I have:

        XmlDocument xmlInput = new XmlDocument();
        /* after this executes, xmlInput has lots of data in it */  
        xmlInput.LoadXml(...); 

        XslCompiledTransform transform = new XslCompiledTransform();
        /* I'm pretty sure Template.xslt is being found and loaded correctly */
        transform.Load(Server.MapPath("/Files/Template.xslt")); 

        using (System.IO.StringWriter sb = new System.IO.StringWriter())
        {
            XmlWriterSettings xSettings = new XmlWriterSettings();
            xSettings.ConformanceLevel = ConformanceLevel.Fragment;
            xSettings.Encoding = Encoding.UTF8;
            /* PROBLEM: After this line, StringWriter sb is {} */
            using (XmlWriter xWriter = XmlWriter.Create(sb, xSettings))
            {
                transform.Transform(xmlInput, xWriter);
            }

            /* xmlText is empty string "" after this line */
            String xmlText = sb.ToString();

            /* ... does more stuff ... */
        }

Can I have some help? I'm really not sure where to go from here to get this working.

EDIT: Even more investigation:

        using (var stringWriter = new StringWriter())
        {
            using (var xmlTextWriter = XmlWriter.Create(stringWriter))
            {
                /* Prints lots of data to the screen */
                Response.Write(xmlInput.InnerXml);

                xmlInput.WriteTo(xmlTextWriter);
                /* Doesn't print anything to the screen */
                Response.Write(stringWriter.GetStringBuilder().ToString() );
            }
        }

BUT, flushing does the trick

                xmlInput.WriteTo(xmlTextWriter);
                xmlTextWriter.Flush();
                /* prints lots of data */
                Response.Write( stringWriter.GetStringBuilder().ToString() );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

何止钟意 2024-12-13 18:32:12

事实证明,我尝试应用的 XSLT 文件需要将 XML 包装在某个元素中。

flush() 技巧很有帮助。

It turns out that the XSLT file I was trying to apply required the XML to be wrapped in a certain element.

But the flush() trick helped a lot.

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