Anyone for Xml Serialization?

Posts   
 
    
Devildog74
User
Posts: 719
Joined: 04-Feb-2004
# Posted on: 29-Nov-2004 20:30:53   

I am trying to work out creating a flexible decorator pattern and visitor pattern. I think I have a decent solution, however in order to make it as flexible as possible I need to have a rather advanced configuration section.

Option 1, create xml serializable object. This could be a night mare due to the complexity and nesting of the xml involved.

Option 2, read the content of the configuration section into an xml document.

Once I have the xml document I can use xpath and such to interpret the contents of the configuration section.

So, I have a class that looks like this:



    public class BaseConfigSectionHandler:IConfigurationSectionHandler,IConfigurationSectionHandlerWriter
    {
        private XmlSerializer _serializer;

        public BaseConfigSectionHandler(XmlSerializer serializer)
        {
                                     _serializer = serializer;
        }
        #region IConfigurationSectionHandler Members

        public object Create(object parent, object configContext, XmlNode section)
        {
            object obj = null;
            obj = _serializer.Deserialize(new StringReader(section.OuterXml));
            return obj;
        }

        #endregion

    }

Then I have another class that Inherits from the class above, and in this derived class, I want to pass an xml serializer to the base class which points to a type of object. The class would look like this:


    public class FileMonitorSectionHandler : BaseConfigSectionHandler
    {
        public FileMonitorSectionHandler():base(new XmlSerializer(typeof(FileMonitorConfig)))
        {
        }

    }

Now, when the create method is called, and instance of FileMonitorConfig would be created and I could interact with the FileMonitorConfig object.

If I wanted to deserialize an xml document instead of FileMonitorConfig, would I simply initialize the base class using base(new XmlSerializer(typeof(XmlDocument))) ?

What happens if you try to use an XmlSerializer to deserialize a string into an XmlDocument?

Thanks in advance

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39826
Joined: 17-Aug-2003
# Posted on: 29-Nov-2004 21:10:44   

Xml serialization is pretty slow, and often (read: always) you end up doing some manual work (or a lot of work) anyway. What's slow is that .NET will generate C# code the first time you deserialize the data, which is probably the only time the data is read, so overall it's thus a slow solution.

Frans Bouma | Lead developer LLBLGen Pro
Devildog74
User
Posts: 719
Joined: 04-Feb-2004
# Posted on: 30-Nov-2004 01:21:54   

Yes of course, I will not dispute the fact that xml seriailization is slow.

However, I am never writing the data. Only reading it from a config file, then caching it in a singleton. There is a watcher on the file that fires and updates the value in the singleton if the file changes. So, I am maximizing speed as much as possible.

Instead of deserializing the data to a custom object, how can I use the xml serializer to serialize an XmlDocument object? (This has nothing to do with llblgen really, its more in line with configuration sections and configuration section handlers)

Thanks.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39826
Joined: 17-Aug-2003
# Posted on: 30-Nov-2004 10:21:18   

Devildog74 wrote:

Instead of deserializing the data to a custom object, how can I use the xml serializer to serialize an XmlDocument object? (This has nothing to do with llblgen really, its more in line with configuration sections and configuration section handlers)

The xml serializer is meant to convert from xml to objects. So if you want to read an xml file into an xml dom, either use the Load method of the Xml dom object or use an xmlreader derived class (xmltextreader for example). With the latter, you can manually browse through the data while being read and act on the nodes you see.

Frans Bouma | Lead developer LLBLGen Pro