470,855 Members | 1,187 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,855 developers. It's quick & easy.

How to auto-serialize null Property as blank Attribute?

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
0 1365

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Manlio Perillo | last post: by
5 posts views Thread by Robert Downes | last post: by
20 posts views Thread by Vijay Kumar R. Zanvar | last post: by
5 posts views Thread by maya | last post: by
22 posts views Thread by nospam_news | last post: by
1 post views Thread by neridaj | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.