如何从XmlReader创建XML文件? [英] How to create an XML file from a XmlReader?

查看:127
本文介绍了如何从XmlReader创建XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何从System.Xml.XmlReader中编写XML文件?

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.

XmlReader对象传达了存储在数据库中的xml,只需要从数据库中取出来即可。一份文件。有什么简单的方法吗?

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


推荐答案

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

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

例如:

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

这篇关于如何从XmlReader创建XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆