CITBL
CITBL

Reputation: 1677

Saving XML to a File while reading it with XmlReader

I receive XML in an HTTP response. I want to parse it with XmlReader because it's rather large and has many child nodes. But in the same time I want to be able to save the whole XML to a file.

How can I do this without first reading the whole XML to a memory buffer?

Also, I do not always parse XML to the last element, but I need to save whole XML.

Thank you

Upvotes: 0

Views: 2718

Answers (3)

Mujtaba Hassan
Mujtaba Hassan

Reputation: 2493

You can create a streamwriter and write node-by-node as you need it.

using (StreamWriter writer = new StreamWriter("file.txt"))
{
        //Start loop to read XML here

    writer.Write("XML node you want to write");     
        // Loop ends
}

Upvotes: 0

Richard Blewett
Richard Blewett

Reputation: 6109

Why not push the incoming stream straight to a file and then parse the XML in the file afterwards using XmlReader?

Upvotes: 1

Ravi Gadag
Ravi Gadag

Reputation: 15851

if i am not wrong, you can use XmlWriter.WriteNode method,

XmlTextWriter empwriter = new XmlTextWriter ();

    //Write the start tag.
    empwriter .WriteStartElement("Employee");

    //Write the first employee.
    empwriter .WriteNode(reader, false);

         //all your elements.. 

    //Write the last employee.
    empwriter.WriteNode(reader, false);
    empwriter.WriteEndElement();

XML Writer

Upvotes: 0

Related Questions