4

Is there a way to have XmlSerializer ignore all members by default, unless I say otherwise?

I have a base class and several derived classes with lots of members, but most I do not want to be serialized. Only a select few are acceptable for serialization.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
morrog
  • 664
  • 1
  • 8
  • 17
  • 1
    No. There is no way to do this. Why are you using XML Serialization? What version of .NET are you using? – John Saunders Sep 30 '09 at 01:29
  • .NET 3.5. I am using XML files to describe cutscenes in a game. I'd like those XML files deserialized into corresponding classes/objects so the game engine can run the cutscene. There will also be re-serialization in the cutscene editor. If there's a better way to do this than XML, I'm open to suggestions. XML+XMLSerializer makes it easy to modify and debug cutscenes by hand, but also easy to implement and work with in the C# code. – morrog Sep 30 '09 at 04:31

2 Answers2

4

No, you cannot do this.

The XmlSerializer is using a "opt-out" process - it will serialize everything (all public properties) unless you explicitly opt-out by using the [XmlIgnore] attribute. There's no way of changing this behavior.

The .NET 3.5 DataContractSerializer on the other hand is taking the other approach - opt-in. It will not serialize anything, unless you specifically tell it to, by decorating your members with [DataMember].

So maybe the DataContract serializer would work for you? It was a few more advantages (doesn't require a parameter-less constructor, can serialize internal and private properties, too, and it can also serialize fields instead of properties, if needed), and it's tuned for speed. There's some downsides, too - it doesn't support attributes in XML nodes - so you'll have to pick based on your requirements.

There's a good comparison of the two by Dan Rigsby - check it out!

Marc

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • A make-or-break factor might be the fact that DataContractSerializer does not support XmlAttributeAttribute. –  Jan 19 '11 at 21:23
1

You could implement IXMLSerializable and determine what you want to be serialized. Here is an example of Object serialization. Check out this SO post about the proper way to implement IXMLSerializable. Here is an example of IXMLSerializable using for some collections.

It would look something like this:

using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace ConsoleApplicationCSharp
{
  public class ObjectToSerialize : IXmlSerializable
  {
    public string Value1;
    public string Value2;
    public string Value3;   
    public string ValueToSerialize;
    public string Value4;
    public string Value5;

    public ObjectToSerialize() { }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
      writer.WriteElementString("Val", ValueToSerialize);
    }

    public void ReadXml(System.Xml.XmlReader reader) 
    {
        if (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == "Event")
        {
            ValueToSerialize = reader["Val"];
            reader.Read();
        }        
    }
    public XmlSchema GetSchema() { return (null); }
    public static void Main(string[] args)
    {
      ObjectToSerialize t = new ObjectToSerialize();
      t. ValueToSerialize= "Hello";
      System.Xml.Serialization.XmlSerializer x = new XmlSerializer(typeof(ObjectToSerialize));
      x.Serialize(Console.Out, t);
      return;
    }
  }
}
Community
  • 1
  • 1
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
  • I thnk writer.WriteFullEndElement(); is not needed in your code, since you have used WriteElementString instead of WriteStartElement. – Anax Sep 30 '09 at 05:46
  • Thanks, I updated it. I guess I should also note I was being a bit lazy, this class wouldnt xml serialize to anything since I dont have any properties. Lets assume my member variables are properties :) – SwDevMan81 Sep 30 '09 at 11:32