473,385 Members | 1,548 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,385 software developers and data experts.

retaining leading whitespace in text of an xml node

when I run the following code the leading spaces of the "data" node
are removed. How do I retain whitespace?

private void CreateBasicXmlDocument()
{
XmlWriterSettings writeSettings = new XmlWriterSettings();
writeSettings.Indent = true;
XmlWriter writer = XmlWriter.Create( filePath, writeSettings );
writer.WriteStartElement("data");
writer.WriteWhitespace(" ");
writer.WriteValue(" data with leading space");
writer.WriteEndElement();
writer.Close();
}

Feb 19 '07 #1
4 6029
Steve,

I think you need to set the XmlSpace attribute of the XmlWriter to
XmlSpace.Preserve.

--BJ

"Steve Richter" wrote:
when I run the following code the leading spaces of the "data" node
are removed. How do I retain whitespace?

private void CreateBasicXmlDocument()
{
XmlWriterSettings writeSettings = new XmlWriterSettings();
writeSettings.Indent = true;
XmlWriter writer = XmlWriter.Create( filePath, writeSettings );
writer.WriteStartElement("data");
writer.WriteWhitespace(" ");
writer.WriteValue(" data with leading space");
writer.WriteEndElement();
writer.Close();
}

Feb 19 '07 #2
On Feb 19, 12:36 pm, BJ Safdie <MSDNNospam...@nospam.nospamwrote:
Steve,

I think you need to set the XmlSpace attribute of the XmlWriter to
XmlSpace.Preserve.
I tried that. the XmlSpace attribute is read only. very confusing!

-Steve

>
--BJ

"Steve Richter" wrote:
when I run the following code the leading spaces of the "data" node
are removed. How do I retain whitespace?
private void CreateBasicXmlDocument()
{
XmlWriterSettings writeSettings = new XmlWriterSettings();
writeSettings.Indent = true;
XmlWriter writer = XmlWriter.Create( filePath, writeSettings );
writer.WriteStartElement("data");
writer.WriteWhitespace(" ");
writer.WriteValue(" data with leading space");
writer.WriteEndElement();
writer.Close();
}- Hide quoted text -

- Show quoted text -

Feb 19 '07 #3
Hi Steve,

The white spaces you add are there. Your code appends a single space character in front of the value.

If you are missing the indentation, bear in mind that WriteWhiteSpace isonly used when manually formatting the xml, which means effectively overriding the indentation. Furthermore, indentation won't happen on the innermost node, and since you only have a single node it won't be broken on two lines. The code below is probably more what you seek.

writer.WriteStartElement("data");
writer.WriteStartElement("innerdata");
writer.WriteValue(" data with leading space");
writer.WriteEndElement();
writer.WriteEndElement();
On Mon, 19 Feb 2007 18:10:35 +0100, Steve Richter <St************@gmail.comwrote:
when I run the following code the leading spaces of the "data" node
are removed. How do I retain whitespace?

private void CreateBasicXmlDocument()
{
XmlWriterSettings writeSettings = new XmlWriterSettings();
writeSettings.Indent = true;
XmlWriter writer = XmlWriter.Create( filePath, writeSettings );
writer.WriteStartElement("data");
writer.WriteWhitespace(" ");
writer.WriteValue(" data with leading space");
writer.WriteEndElement();
writer.Close();
}



--
Happy coding!
Morten Wennevik [C# MVP]
Feb 19 '07 #4
On Feb 19, 1:01 pm, "Morten Wennevik [C# MVP]"
<MortenWenne...@hotmail.comwrote:
Hi Steve,

The white spaces you add are there. Your code appends a single space character in front
of the value.
your right! <smacks head I was displaying the xml document thru
Windows Explorer -Internet Explorer. Whatever font it was in really
shrunk down the whitespace.

thanks,

-Steve

Feb 20 '07 #5

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

Similar topics

1
by: JayC | last post by:
Hey guys, I've noticed that if you have an XML snippet structured like this (I'm using square brackets instead in case it messes up and HTML readers): Blah Blah 2
1
by: John | last post by:
I have been trying with no result, to force Xerces parsers to ignore whitespace in my XML file. I have tried several of the NG suggested setFeature() methods in DOMParser to no avail. I have now...
3
by: Celedor | last post by:
If I understand correctly, canonicalized XML is a simplified, or rather, "standardized" form of XML. It is in such a form such that two documents that are written in different ways, but contain...
2
by: Wired Earp | last post by:
I've had some luck using string values "\t" "\n" and "\r" to insert tabs, newlines and carriagereturn textnodes into a document, but I can't *read* these nodes, at least not by analyzing the...
8
by: Tjerk Wolterink | last post by:
Hello all, how does xsl handle white space? I know you can set domething like this for nice indentation: <xsl:output method="xhtml" indent="yes"/> But know i have xsl code like this:
2
by: Carlitos | last post by:
Hi there, A class in Xerces J-API (Java) called TextImpl contains a property that returns whether the text is ignorable whitespace...
9
by: amattie | last post by:
Does anyone have any idea on how I can strip the extra whitespace in the XML that shows up when I receive a response from an ASP.NET 2.0 webservice? This has been discussed before, but no one has...
1
by: Steve Richter | last post by:
when I run the following code the leading spaces of the "data" node are removed. How do I retain whitespace? private void CreateBasicXmlDocument() { XmlWriterSettings writeSettings = new...
9
by: martymix | last post by:
simple question: I have a simple <dt>test text</dt> I get the innerHTML of that dt, and I try and append some text to it like so: dt = document.getElementsByTagName('dt') var text =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...

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.