By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,962 Members | 2,545 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,962 IT Pros & Developers. It's quick & easy.

retaining leading whitespace in text of an xml node

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.