views:

15

answers:

1

I am implementing a class object that is serializable.

something like this:

  StringWriter sw = new StringWriter();
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("", "");

        System.Xml.Serialization.XmlSerializer x = 
              new System.Xml.Serialization.XmlSerializer(this.GetType());
        x.Serialize(sw, this,ns);

        return sw.ToString();

I want the child class/objects to have different names depending on a flag. is there a ways to change the ElementName of the class prop. during run time without write a custom WriteXml?

A: 

Yes, but you won't like it. IXmlSerializable

Steven Sudit
Yes, I saw that as an option. But seems liek too much code.
Vilasack
It would be nice if somehow that we can make the attb of the prop conditional based on some flag.
Vilasack
That's not viable, because the whole point of XmlSerializer is that it reflects through your code JIT, then caches what it finds. Even if you could change attributes on the fly, the changes would be ignored. Sorry.
Steven Sudit
Actually, maybe you can hack around it. There are ways to control whether an element is emitted. What if you had two properties, each with their own element name, and only used one at a time?
Steven Sudit
Yeah that is what I figured. My other option was to create two duplicate class structures. one with a different set of element names. (YUK!)
Vilasack
Output 1: <product>value</product>Output 2: <a100>value</a100>this would be genereated from the same object
Vilasack
Can I programmatically turn on and off the xmlIgnore attr?
Vilasack
No, that's not the way I meant. I'm talking about ShouldSerialize* and *Specified.
Steven Sudit
See http://horacegoescoding.blogspot.com/2009/04/using-shouldserialize-for-conditional.html
Steven Sudit
Also see: http://stackoverflow.com/questions/592671
Steven Sudit