I've discovered that if a serializable Field/Property has a corresponding field of type Boolean having as a name the Field/Property name with "Specified" suffix, the XmlSerializer conditionally exclude that Field/Property from the serialization process. Nice!
So, I want to avoid the definition of those fields, and add them dynamically, at runtime...
Reading this, I found an interesting interface IReflect, which I can use to "emulate" dynamic fields which are used by the XmlSerializer instances to exclude certain fields.
Would this work?
Yes, you can tell the XmlSerializer to ignore namespaces during de-serialization. Note this is the kind of thing I meant. You are not telling the XmlSerializer to ignore namespaces - you are giving it XML that has no namespaces.
The XmlSerializer creates C# (. cs) files and compiles them into . dll files in the directory named by the TEMP environment variable; serialization occurs with those DLLs. These serialization assemblies can be generated in advance and signed by using the SGen.exe tool.
Since XmlSerializer is one of the few thread safe classes in the framework you really only need a single instance of each serializer even in a multithreaded application.
The XmlElementAttribute belongs to a family of attributes that controls how the XmlSerializer serializes or deserializes an object. For a complete list of similar attributes, see Attributes That Control XML Serialization.
I will extend answer of Martin Peck.
You can avoid serialization of the fields/properties with "Specified" suffix.
You should define that "*Specified" properties in your class and apply [XmlIgnoreAttribute()]
to them.
Here is an example:
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://yournamespace.com")]
public partial class YourObject
{
private long sessionTimeoutInSecondsField;
private bool sessionTimeoutInSecondsFieldSpecified;
public long sessionTimeoutInSeconds
{
get
{
return this.sessionTimeoutInSecondsField;
}
set
{
this.sessionTimeoutInSecondsField = value;
}
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool sessionTimeoutInSecondsSpecified
{
get
{
return this.sessionTimeoutInSecondsFieldSpecified;
}
set
{
this.sessionTimeoutInSecondsFieldSpecified = value;
}
}
}
If you want to take control of your xml serialization then you have two options. The first (which might not be appropriate here) it to use the attributes in the System.Xml.Serialization namespace to exclude properties. If you really need to do determine what gets serialized at runtime this might not be the best course of action.
See Attributes That Control XML Serialization
The other way to do this is to implement the IXmlSerializable interface on your class and implement the ReadXml and WriteXml methods. This allows you to take control of exactly how your xml looks. See this question for additional info:
custom xml serialization
However, as mentioned here Mixing custom and basic serialization? once you implement IXmlSerializable you are responsible for all the serialization logic for your type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With