如何从 XmlReader 创建 XML 文件?

发布于 2024-09-28 04:08:12 字数 438 浏览 1 评论 0原文

如何从 System.Xml.XmlReader 写入 XML 文件?

我认为这是一个简单的问题,但每当我搜索时,我似乎最终都会将文件读取给读取器或逐个节点写入。

XmlReader 对象传送存储在数据库中的 xml,只需从数据库中取出到文件即可。有什么简单的方法可以做到这一点吗?

        SqlCommand dataCmd = new SqlCommand(sqlText, Conn);
        System.Xml.XmlReader dataReader = null;

        dataCmd.CommandTimeout = 60000;

        Conn.Open();
        dataReader = dataCmd.ExecuteXmlReader();
        dataReader.Read();

How do you write an XML file from an System.Xml.XmlReader?

I thought this would be a simple question but whenever I search I seem to be ending up with reading the file to a reader or writing node by node.

The XmlReader object conveys xml that was stored in a database and just needs to come out of the database to a file. Is there any easy way to do this?

        SqlCommand dataCmd = new SqlCommand(sqlText, Conn);
        System.Xml.XmlReader dataReader = null;

        dataCmd.CommandTimeout = 60000;

        Conn.Open();
        dataReader = dataCmd.ExecuteXmlReader();
        dataReader.Read();

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

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

发布评论

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

评论(2

滥情哥ㄟ 2024-10-05 04:08:13

您需要创建一个 XmlWriter 并调用其 WriteNode 方法

例如:

using (conn)
using (SqlCommand dataCmd = new SqlCommand(sqlText, Conn)) {
    dataCmd.CommandTimeout = 60000;

    Conn.Open();
    using (XmlReader dataReader = dataCmd.ExecuteXmlReader())
    using (XmlWriter writer = XmlWriter.Create(File.OpenWrite(...)) {
        writer.WriteNode(dataReader, true);
    }
}

You need to create an XmlWriter and call its WriteNode method.

For example:

using (conn)
using (SqlCommand dataCmd = new SqlCommand(sqlText, Conn)) {
    dataCmd.CommandTimeout = 60000;

    Conn.Open();
    using (XmlReader dataReader = dataCmd.ExecuteXmlReader())
    using (XmlWriter writer = XmlWriter.Create(File.OpenWrite(...)) {
        writer.WriteNode(dataReader, true);
    }
}
一世旳自豪 2024-10-05 04:08:13

最简单的方法是将其传递到 XmlWriter,使用如下方法:

public void WriteOutXml(XmlReader xmlReader, string fileName)
{
    var writer = XmlWriter.Create(fileName);
    writer.WriteNode(xmlReader, true);
}

The simplest way would be to pass it into an XmlWriter, using a method such as this:

public void WriteOutXml(XmlReader xmlReader, string fileName)
{
    var writer = XmlWriter.Create(fileName);
    writer.WriteNode(xmlReader, true);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文