C# 可以使用 XmlWriter 将 XML 写入内存吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/683189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 13:17:21  来源:igfitidea点击:

Possible to write XML to memory with XmlWriter?

c#asp.netxmlxmlwriter

提问by MetaGuru

I am creating an ASHX that returns XML however it expects a path when I do

我正在创建一个返回 XML 的 ASHX,但是当我这样做时它需要一个路径

XmlWriter writer = XmlWriter.Create(returnXML, settings)

But returnXML is just an empty string right now (guess that won't work), however I need to write the XML to something that I can then send as the response text. I tried XmlDocument but it gave me an error expecting a string. What am I missing here?

但是 returnXML 现在只是一个空字符串(猜想这行不通),但是我需要将 XML 写入一些内容,然后我可以将其作为响应文本发送。我试过 XmlDocument 但它给了我一个错误,需要一个字符串。我在这里缺少什么?

采纳答案by Jon Skeet

If you really want to write into memory, pass in a StringWriteror a StringBuilderlike this:

如果你真的想写入内存,像这样传入 aStringWriter或 a StringBuilder

using System;
using System.Text;
using System.Xml;

public class Test
{
    static void Main()
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;        
        StringBuilder builder = new StringBuilder();

        using (XmlWriter writer = XmlWriter.Create(builder, settings))
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("root");
            writer.WriteStartElement("element");
            writer.WriteString("content");
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
        }
        Console.WriteLine(builder);
    }
}

If you want to write it directly to the response, however, you could pass in HttpResponse.Outputwhich is a TextWriterinstead:

但是,如果您想将其直接写入响应,则可以传入HttpResponse.Outputwhich is a TextWriter

using (XmlWriter writer = XmlWriter.Create(Response.Output, settings))
{
    // Write into it here
}

回答by Steve

The best way to do that is to write directly to the Response Output Stream. Its a stream that's built-in to ASP.NET to allow you to write whatever output as a stream, in this case you can write XML to it.

最好的方法是直接写入响应输出流。它是一个内置于 ASP.NET 的流,允许您将任何输出编写为流,在这种情况下,您可以将 XML 写入它。

回答by BFree

    StringBuilder xml = new StringBuilder();
    TextWriter textWriter = new StringWriter(xml);
    XmlWriter xmlWriter = new XmlTextWriter(textWriter);

Then, use the xmlWriter to do all the xml writing, and that writes it directly to the StringBuilder.

然后,使用 xmlWriter 完成所有 xml 写入,并将其直接写入 StringBuilder。

Edit: Thanks to Jon Skeet's comment:

编辑:感谢 Jon Skeet 的评论:

    StringBuilder xml = new StringBuilder();
    XmlWriter xmlWriter = XmlWriter.Create(xml);

回答by Lo?c

Something was missing on my side: flushing the XmlWriter's buffer:

我这边缺少一些东西:刷新 XmlWriter 的缓冲区:

static void Main()
{
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;        
    StringBuilder builder = new StringBuilder();

    using (XmlWriter writer = XmlWriter.Create(builder, settings))
    {
        writer.WriteStartDocument();
        writer.WriteStartElement("root");
        writer.WriteStartElement("element");
        writer.WriteString("content");
        writer.WriteEndElement();
        writer.WriteEndElement();
        writer.WriteEndDocument();

        writer.Flush();

    }
    Console.WriteLine(builder);
}