473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Writing formatted XML

Dan
How would I write an XmlDocument to a file so that each node is properly
indented and followed by a carriage return?

Thanks...Dan
Nov 12 '05 #1
1 2910
http://msmvps.com/coad/archive/2004/06/22/8692.aspx
Using XmlDocument.Save(string file) produces a file with nicely indented
elements. XmlDocument.OuterXml returns a string without any formatting. If
you want a nicely formatted string (to display to the user, write to
console, etc), without directly writing to a file, you must use the
XmlTextWriter.
using System.IO;
using System.Xml;

public static string FormatXML(XmlDocument doc)
{
// Create a stream buffer that can be read as a string
using (StringWriter sw = new StringWriter())

// Create a specialized writer for XML code
using (XmlTextWriter xtw = new XmlTextWriter(sw))
{
// Set the writer to use indented (hierarchical) elements
xtw.Formatting = System.Xml.Formatting.Indented;

// Write the XML document to the stream
doc.WriteTo(xtw);

// Return the stream as a string
return sw.ToString();
}
}

"Dan" <Da*@discussions.microsoft.com> wrote in message
news:E6**********************************@microsof t.com...
How would I write an XmlDocument to a file so that each node is properly
indented and followed by a carriage return?

Thanks...Dan

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: hpy_awad | last post by:
I am writing stings ((*cust).name),((*cust).address)to a file using fgets but rabish is being wrote to that file ? Look to my source please and help me finding the reason why this rabish is being...
2
by: C.H. | last post by:
Hello, I am writing to a .txt file. Lots of data, about 1.1MB. It only took less than a second for VB6 to do so, but it took VB.NET one minute!! I am using a simple fileopen and print. Does...
7
by: utab | last post by:
Hi there, I am trying to read from a file and at the same time change certain fields of the same field, there are 6 fields in this file like 1 2 3 4 5 6...
6
by: redcic | last post by:
Hi all, I use the csv module of Python to write a file. My code is of the form : cw = csv.writer(open("out.txt", "wb")) cw.writerow() cw.writerow() And i get an out.txt file looking like:
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.