473,624 Members | 2,288 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XmlTextWriter Formatting and PreserveWhiteSp ace

Hi

I'm reading an xml file into an XmlDocument, adding some nodes, and writing
it back out. I would like the nodes I add to assume the same level of
indeting as the rest of the document. (I load the document with
PreserveWhiteSp ace = true.)

I thought I could do this by using an XmlTextWriter with Formatting set to
Formatting.Inde nted, but this doesn't work. The Formatting.Inde nted on the
XmlTextWriter only indents my output when I initially load the document with
PreserveWhiteSp ace = false. But that defeats my object, as I get nicely
formatted XML, but without the (nice) extra whitespace I had when I first
loaded the XML file.

Is there any XmlTextWriter class that will indent my XML properly according
to already existing whitespace?

I used the following code:

if (openFileDialog 1.ShowDialog(th is) == DialogResult.OK )
{
xmldoc.Preserve Whitespace = true;
xmldoc.Load(ope nFileDialog1.Fi leName);
StringWriter sw = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(s w);
writer.Formatti ng = Formatting.Inde nted;
xmldoc.Save(wri ter);
writer.Close();
richTextBox1.Te xt = sw.ToString();
}

This only works if I set the PreserveWhiteSp ace property to false before I
load the document. Even if I set preserveWhiteSp ace to false before I call
the Save method, it doesn't work! Any help please?

Riko
Nov 12 '05 #1
1 10179
Hi Riko,

The problem you are running into is that once the XmlTextWriter sees a
whitespace in the element content, it turns off the indentation for that
element and its children. That’s because it makes the element to have a mixed
content and we should not indent it. So nothing gets indented under an
element that has a white space (or text node) child, which happens when you
load with preserveWhitesp ace = true. With preserveWhitesp ace = false
everything should get correctly indented..

-Helena Kupkova, Webdata Team, Microsoft
"Riko Eksteen" wrote:
Hi

I'm reading an xml file into an XmlDocument, adding some nodes, and writing
it back out. I would like the nodes I add to assume the same level of
indeting as the rest of the document. (I load the document with
PreserveWhiteSp ace = true.)

I thought I could do this by using an XmlTextWriter with Formatting set to
Formatting.Inde nted, but this doesn't work. The Formatting.Inde nted on the
XmlTextWriter only indents my output when I initially load the document with
PreserveWhiteSp ace = false. But that defeats my object, as I get nicely
formatted XML, but without the (nice) extra whitespace I had when I first
loaded the XML file.

Is there any XmlTextWriter class that will indent my XML properly according
to already existing whitespace?

I used the following code:

if (openFileDialog 1.ShowDialog(th is) == DialogResult.OK )
{
xmldoc.Preserve Whitespace = true;
xmldoc.Load(ope nFileDialog1.Fi leName);
StringWriter sw = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(s w);
writer.Formatti ng = Formatting.Inde nted;
xmldoc.Save(wri ter);
writer.Close();
richTextBox1.Te xt = sw.ToString();
}

This only works if I set the PreserveWhiteSp ace property to false before I
load the document. Even if I set preserveWhiteSp ace to false before I call
the Save method, it doesn't work! Any help please?

Riko

Nov 12 '05 #2

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

Similar topics

5
9156
by: Jain, Pranay Kumar | last post by:
Hello Everyone, I have written a simple app. that converts the dataset into excelspreadsheet. The App. uses the following architecture. First it generates the dataset with corresponding datatables(each datatable represents a worksheet).It uses an Xslt file to do the transformation of the xml (generate from dataset) and write the output xml into a xmltextwriter. The App converts the data into excel with almost all of the formatting
2
2056
by: vector | last post by:
Here is a function more or less exactly as I found it from somewhere on the internet. static string BeautifyXML(string sXML) { string result = ""; System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Xml.XmlTextWriter w = new System.Xml.XmlTextWriter(ms, System.Text.Encoding.Unicode);
1
1945
by: st | last post by:
Hi, I've a routine that exports a DB query to Excel by building an XmlDocument and saving to a XmlTextWriter. The function works but new lines don't show up, i.e. significant whitespace is cut. I've tried adding WriteAttributeString("xml", "space", null, "preserve") to the XmlTextWriter just prior to save, but that results in the error: "Index was outside the bounds of the array."
2
7583
by: Steve | last post by:
I'm an XML newb. I'm serializing a class and when I inspect the xml file, all the data is on one line rather than being nested and indented Is that normal? <code> StreamWriter sw = new StreamWriter(m_optionsXmlFilePath, false); XmlSerializer xmlSerializer = new XmlSerializer(typeof(Options)); XmlTextWriter xmlTextWriter = new XmlTextWriter(sw);
2
8848
by: Ghost | last post by:
Can anybody how do I use XmlTextWriter example? I want to write such XML file: <?xml version="1.0" standalone="yes"?> <Clients> <Client> <Name>David</Name> <Age>27</Age> </Client>
1
2768
by: Chris | last post by:
Hi, I'm trying to specify single-quotes to be used in xmlAttributes as follows : XmlTextWriter writer = new XmlTextWriter (filename, null); //Use indenting for readability. writer.Formatting = Formatting.Indented; writer.QuoteChar = (char)39;
2
6317
by: Ken Wilson | last post by:
I am writing and .xml file and am not getting the formatting I would like. The portion of the code that is giving me problems is as follows; XmlTextWriter tw = new XmlTextWriter(filename); tw.Formatting = Formatting.Indented; tw.WriteStartDocument(); tw.WriteStartElement("MyRoot"); tw.WriteStartElement("MyString);
1
2409
by: st | last post by:
Hi, I've a routine that exports a DB query to Excel by building an XmlDocument and saving to a XmlTextWriter. I'm having trouble with carriage returns in a mailing address not showing up in the final Excel sheet. What's added to the InnerText of an XmlDocument is something similar to:
5
3265
by: Gilgamesh | last post by:
Hello, How do I use this class to create an XML document without saving it into a file? I need to store the XML documen into a string so it could be passed as a parameter to another class. Gilgamesh
0
8240
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8625
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8482
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7168
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.