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

Losing space character value when writing attribute

P: n/a
Hi,

Wonder if anyone can help me on this, In DotNet2.0 I've been quite happily
using the WriteAttributeString method of XmlWriter object, but have run into
a problem when trying to output a space character as the value of an
attribute.

Prototype of the method I am using is XmlWriter.WriteAttributeString
(String, String) in the following fashion:

xmlTextWriter.WriteStartElement("Rule");
xmlTextWriter.WriteAttributeString("Separator", " ");
xmlTextWriter.WriteEndElement();

Which results in the following XML:

<Rule Separator="" />

I've looked into xmlTextWriter.WriteWhiteSpace(string) but this outputs
directly into the text writer object, which doesn't help when trying to
write a value for an attribute of an element. I've also checked the other
properties to see if there is a "preserve white space" setting, which I
can't see.

Can anyone throw any light on a possible solution? Can I encode the space
differently to make sure it is kept in the output XML when using
WriteAttributeString or do I have to use a different method (which one?) or
a completely different object for outputting the XML?

Regards,

- Paul.


May 17 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
I cannot reproduce the problem, provide sample code please.

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.XmlLab.Net | http://www.XLinq.Net | http://blog.tkachenko.com

Paul Hadfield wrote:
Hi,

Wonder if anyone can help me on this, In DotNet2.0 I've been quite happily
using the WriteAttributeString method of XmlWriter object, but have run into
a problem when trying to output a space character as the value of an
attribute.

Prototype of the method I am using is XmlWriter.WriteAttributeString
(String, String) in the following fashion:

xmlTextWriter.WriteStartElement("Rule");
xmlTextWriter.WriteAttributeString("Separator", " ");
xmlTextWriter.WriteEndElement();

Which results in the following XML:

<Rule Separator="" />

I've looked into xmlTextWriter.WriteWhiteSpace(string) but this outputs
directly into the text writer object, which doesn't help when trying to
write a value for an attribute of an element. I've also checked the other
properties to see if there is a "preserve white space" setting, which I
can't see.

Can anyone throw any light on a possible solution? Can I encode the space
differently to make sure it is kept in the output XML when using
WriteAttributeString or do I have to use a different method (which one?) or
a completely different object for outputting the XML?

Regards,

- Paul.

May 17 '06 #2

P: n/a
Example code, just the relevant bits from the more detailed document is:

string fileName = "Test.xml";
string separator = " ";
XmlTextWriter xmlTextWriter = new XmlTextWriter(new StreamWriter(fileName,
false, System.Text.Encoding.ASCII));

xmlTextWriter.Formatting = Formatting.Indented;
xmlTextWriter.WriteStartDocument();

xmlTextWriter.WriteStartElement("TransformationRul e");

xmlTextWriter.WriteAttributeString("Delimiter", separator);

xmlTextWriter.WriteEndElement();

xmlTextWriter.WriteEndElement();

results in the attribute Delimiter being written out as

<TransformationRule Delimiter="" /> rather than <TransformationRule
Delimiter=" " />

Please note that the only issue is that the space is lost in the attribute.
"Oleg Tkachenko [MVP]" <so**@body.com> wrote in message
news:eV**************@TK2MSFTNGP02.phx.gbl...
I cannot reproduce the problem, provide sample code please.

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.XmlLab.Net | http://www.XLinq.Net | http://blog.tkachenko.com

Paul Hadfield wrote:
Hi,

Wonder if anyone can help me on this, In DotNet2.0 I've been quite
happily using the WriteAttributeString method of XmlWriter object, but
have run into a problem when trying to output a space character as the
value of an attribute.

Prototype of the method I am using is XmlWriter.WriteAttributeString
(String, String) in the following fashion:

xmlTextWriter.WriteStartElement("Rule");
xmlTextWriter.WriteAttributeString("Separator", " ");
xmlTextWriter.WriteEndElement();

Which results in the following XML:

<Rule Separator="" />

I've looked into xmlTextWriter.WriteWhiteSpace(string) but this outputs
directly into the text writer object, which doesn't help when trying to
write a value for an attribute of an element. I've also checked the
other properties to see if there is a "preserve white space" setting,
which I can't see.

Can anyone throw any light on a possible solution? Can I encode the
space differently to make sure it is kept in the output XML when using
WriteAttributeString or do I have to use a different method (which one?)
or a completely different object for outputting the XML?

Regards,

- Paul.


May 17 '06 #3

P: n/a
Ok, I get it. You must be viewing resulting XML in Internet Explorer and
IE shows empty attributes this way. Take a look into sources.
--
Oleg Tkachenko [XML MVP, MCAD]
http://www.XmlLab.Net | http://www.XLinq.Net | http://blog.tkachenko.com
May 17 '06 #4

P: n/a
Duh!, what a school boy error! Thanks for bearing with me, sure enough
source is fine!

Cheers,

- Paul.

"Oleg Tkachenko [MVP]" <so**@body.com> wrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...
Ok, I get it. You must be viewing resulting XML in Internet Explorer and
IE shows empty attributes this way. Take a look into sources.
--
Oleg Tkachenko [XML MVP, MCAD]
http://www.XmlLab.Net | http://www.XLinq.Net | http://blog.tkachenko.com

May 17 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.