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

How to auto-serialize null Property as blank Attribute?

P: n/a
Hi,

I'm serializing C# objects, only I'm creating attribute-normal XML
instead of (the default) element-normal XML.

For each item that gets serialized, I'm programmatically applying an
XMLAttributeAttribute, to cause each XML Element to, instead, be
serialized as an XML Attribute.

The only problem comes in where non-numeric properties, with null
values, are concerned.

In the case of an unassigned number Property or Field, the output XML
has an attribute with value "0". This is fine with me.

In the case of an unassigned string, however, the output XML acts as if
that attribute does not exist at all. (This is no good since these
objects are going to a client-side deserializer -- and just because a
Property is null, doesn't mean it shouldn't be included in the schema.)

I don't have access to the source code of the classes, so I can't
dictate that the default value of a class should never be null, or
anything like that. I'd like to avoid using Reflection to change null
values to String.Empty, or anything like that -- since that opens up
several new cans of worms.

Are there any other attributes of the serializer that can be set, so
that for a null non-numeric attribute "MyField", something like
"MyField = ' ' " goes into the output XML?

Hope this makes sense -- and thanks for any thoughts!
rs

Apr 12 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.