all groups > dotnet xml > may 2006 >
You're in the

dotnet xml

group:

Losing space character value when writing attribute


Losing space character value when writing attribute Paul Hadfield
5/17/2006 12:00:00 AM
dotnet xml:
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.



Re: Losing space character value when writing attribute Paul Hadfield
5/17/2006 2:08:54 PM
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("TransformationRule");



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.


[quoted text, click to view]
Re: Losing space character value when writing attribute Paul Hadfield
5/17/2006 3:17:30 PM
Duh!, what a school boy error! Thanks for bearing with me, sure enough
source is fine!

Cheers,

- Paul.

[quoted text, click to view]

Re: Losing space character value when writing attribute Oleg Tkachenko [MVP]
5/17/2006 3:51:31 PM
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

[quoted text, click to view]
Re: Losing space character value when writing attribute Oleg Tkachenko [MVP]
5/17/2006 4:24:18 PM
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]
AddThis Social Bookmark Button