Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XML StringWriter为空_C#_Xml_Asp.net 3.5_Stringwriter - Fatal编程技术网

C# XML StringWriter为空

C# XML StringWriter为空,c#,xml,asp.net-3.5,stringwriter,C#,Xml,Asp.net 3.5,Stringwriter,我在XMLDocument中有一些XML,我想把它放到StringWriter中,但它总是变为空(?)。我试着用debug检查变量,但我仍然不知道发生了什么 以下是我所拥有的: XmlDocument xmlInput = new XmlDocument(); /* after this executes, xmlInput has lots of data in it */ xmlInput.LoadXml(...); X

我在
XMLDocument
中有一些XML,我想把它放到
StringWriter
中,但它总是变为空(?)。我试着用debug检查变量,但我仍然不知道发生了什么

以下是我所拥有的:

        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() );

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


但是
flush()
技巧帮了大忙

调用
WriteTo
后调用
xmlTextWriter.Flush()
。当你说“什么都没显示”时,你的意思是调用
WriteTo
清除了一切吗?或者只是当你调用
WriteTo
时,它没有写其他东西?