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] "Oleg Tkachenko [MVP]" <some@body.com> wrote in message
news:eVp7JCbeGHA.3556@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.
>>
>>
>>