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();
XmlReader provides forward-only, read-only access to XML data in a document or stream. This class conforms to the W3C Extensible Markup Language (XML) 1.0 (fourth edition) and the Namespaces in XML 1.0 (third edition) recommendations. XmlReader methods let you move through XML data and read the contents of a node.
To summarize: An XML file is a file used to store data in the form of hierarchical elements. Data stored in XML files can be read by computer programs with the help of custom tags, which indicate the type of element.
C# XmlDocument tutorial shows how to work with XML in C# with XmlDocument. Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. XML is often used for application configuration, data storage and exchange.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With