Thread: XmlSerializer help

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    XmlSerializer help

    I am using the XmlSerializer class to serialize a class that derives from Form. Currently I have a class that links to the properties in the derived class to serialize them because serializing the properties in the Form class obviusly doesn't work. However I would much rather just serialize the intance of my derived class. My thought on this is to make it only serialize the properties in my derived class (so it won't touch the properties in Form), but I tried many ways but nothing worked.

    Any ideas how to do this, and if it is imposible to do it the way I want any pointers on how else I should go about this problem?

  2. #2
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Alright I fixed it. Before I got to far into my other plan I was reading and found out about XmlAttributeOverrides. Well this does the trick along with a little reflection. What I did was "virtualy" mark all properties and fields in the Form class (and the classes that Form derives from) with the XmlIgnore attribute. Therefore it only serializes the properties and fields that I created in my class that derives from Form. Heres the code I used to do it:

    Code:
    XmlAttributeOverrides over = new XmlAttributeOverrides();
    
    XmlAttributes attributes = new XmlAttributes();
    attributes.XmlIgnore = true;
    
    Type type = typeof(Form);
    while (type != null)
    {
        foreach (PropertyInfo info in type.GetProperties())
        {
            over.Add(type, info.Name, attributes);
        }
    
        foreach (FieldInfo info in type.GetFields())
        {
            over.Add(type, info.Name, attributes);
        }
    
        type = type.BaseType;
    }
    And than I created the XmlSerializer like this:

    Code:
    XmlSerializer serializer = new XmlSerializer(typeof(Options), over);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. XMLSerializer
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-13-2008, 08:10 AM