tags:

views:

58

answers:

1

Say I have an xml file like:

<Filters>
    <Blur Name="Blur01" />
    <Sharpen Name="Sharpen01" Amount=5 />
</Filters>

How do I set the properties of Blur, Sharpen, etc without a performance hit? I thought reflection would work, but the number of nodes and properties will be in millions, so the reflection cost would be very high IMO.

So say I get a list of properties like:

{ "Name", "Amount", ... }

How do I call them on an already initialized object?

I also have the option to pass them to optional arguments as I will be constructing the objects from the xml at the time of reading it.

+4  A: 

Use the built-in XML serialization in .NET. If you have performance problems, consider pre-generating the parsers with SGEN.exe.

XML Serializer in .NET

XML Serializer Generator Tool (Sgen.exe)

If you parse yourself, reflection is not terribly expensive if you keep the PropertyInfo objects around.

Finally, you could just write your own parser... but in reality, your XML Serialization that .NET gives works pretty damn well.

Brian Genisio
Pregenerating XML serialization assemblies does nothing for runtime performance, only improves startup/first use time.
Anton Tykhyy
@Anton The first time you use it per execution, that is. How is that not runtime?
Brian Genisio
Thanks Anton, can I use the xml serialization for setting properties of objects?
Joan Venge
Absolutely. You decorate an object with the XML tag names and tell the deserializer to generate one from XML. Out comes an object with all the properties set. Magic.
Brian Genisio
Thanks Brian, actually I meant Brian in the previous post. I will try that one now.
Joan Venge
Also Brian, would using xml serialization have a problem if I later rename Sparpen.Amount to something else? I bet I could keep the old parameter internal?
Joan Venge