How to create an XML file from a XmlReader?

后端 未结 2 914
傲寒
傲寒 2020-12-11 02:47

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 fi

相关标签:
2条回答
  • 2020-12-11 02:50

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-11 03:00

    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);
    }
    
    0 讨论(0)
提交回复
热议问题