0

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?

H H
  • 263,252
  • 30
  • 330
  • 514

1 Answers1

0

Yes, but you won't like it. IXmlSerializable

Steven Sudit
  • 19,391
  • 1
  • 51
  • 53
  • It would be nice if somehow that we can make the attb of the prop conditional based on some flag. –  Sep 09 '10 at 19:48
  • 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 Sep 09 '10 at 19:49
  • 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 Sep 09 '10 at 19:50
  • 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!) –  Sep 09 '10 at 19:51
  • Output 1: value Output 2: value this would be genereated from the same object –  Sep 09 '10 at 19:52
  • Can I programmatically turn on and off the xmlIgnore attr? –  Sep 09 '10 at 19:55
  • No, that's not the way I meant. I'm talking about ShouldSerialize* and *Specified. – Steven Sudit Sep 09 '10 at 20:12
  • See http://horacegoescoding.blogspot.com/2009/04/using-shouldserialize-for-conditional.html – Steven Sudit Sep 09 '10 at 20:12