I have an InputConfig class that contains a List<IncludeExcludeRule> :
public class InputConfig { // The rest of the class omitted private List<IncludeExcludeRule> includeExcludeRules; public List<IncludeExcludeRule> IncludeExcludeRules { get { return includeExcludeRules; } set { includeExcludeRules = value; } } } public class IncludeExcludeRule { // Other members omitted private int idx; private string function; public int Idx { get { return idx; } set { idx = value; } } public string Function { get { return function; } set { function = value; } } }
Using...
FileStream fs = new FileStream(path, FileMode.Create); XmlSerializer xmlSerializer = new XmlSerializer(typeof(InputConfig)); xmlSerializer.Serialize(fs, this); fs.Close();
... and ...
StreamReader sr = new StreamReader(path); XmlSerializer reader = new XmlSerializer(typeof(InputConfig)); InputConfig inputConfig = (InputConfig)reader.Deserialize(sr);
He works like a champion! Lightweight material, except that when deserializing I have to keep spaces in the function element. The generated XML file demonstrates that spaces remained during serialization, but it is lost during deserialization.
<IncludeExcludeRules> <IncludeExcludeRule> <Idx>17</Idx> <Name>LIEN</Name> <Operation>E =</Operation> <Function> </Function> </IncludeExcludeRule> </IncludeExcludeRules>
The MSDN documentation for XmlAttributeAttribute seems to refer to this same problem under the heading Notes , but I donβt understand how to use it. It provides this example:
// Set this to 'default' or 'preserve'. [XmlAttribute("space", Namespace = "http://www.w3.org/XML/1998/namespace")] public string Space
A? Ask what to do with "default" or "save"? I'm sure I'm close, but that just doesn't make sense. I need to think that only one line of XmlAttribute should be inserted into the class before the member to preserve the space when deserializing.
There are many examples of similar issues here and elsewhere, but they all seem to be related to using XmlReader and XmlDocument, or to tricking with individual nodes, etc. I would like to avoid this depth.