Hi Gabriel Sas,
If I understand you rigth you want a field's value to be read from xml but not written during serialization. If you don't want to do the serialization yourself (write some read/write methods fitting your needs - or whatever) maybe I have an idea: Use combination of XmlIgnore and a second field for serialization. Look at this example:
Caution: I don't say this is an good idea or should be done like that, was just a little experiment from my first thought, and seems to work...
using System;
using System.IO;
using System.Xml.Serialization;
namespace ExcludeOnlyFromSerialization
{
class Program
{
static void Main(string[] args)
{
const string m_strPATH = "Test.xml";
SampleClass sampleobject = new SampleClass() { SomeString = "Test", SomeInteger = 987, SomeBoolean = true };
Console.WriteLine(sampleobject);
Save(sampleobject, m_strPATH);
SampleClass sampleobjectNew = Load(m_strPATH);
Console.WriteLine(sampleobjectNew);
Console.ReadKey();
}
static SampleClass Load(string strFilePath)
{
SampleClass scLoaded = null;
if (File.Exists(strFilePath))
{
XmlSerializer serializer = new XmlSerializer(typeof(SampleClass));
using (FileStream filestream = new FileStream(strFilePath, FileMode.Open))
{
try
{
scLoaded = (SampleClass)serializer.Deserialize(filestream);
}
catch
{
throw;
}
finally
{
filestream.Close();
}
}
}
return scLoaded;
}
static void Save(SampleClass sc, string strFilePath)
{
XmlSerializer serializer = new XmlSerializer(typeof(SampleClass));
using (TextWriter textwriter = new StreamWriter(strFilePath))
{
try
{
serializer.Serialize(textwriter, sc);
}
catch (Exception)
{
throw;
}
finally
{
textwriter.Close();
}
}
}
}
public class SampleClass
{
[XmlIgnore]
public string SomeString { get; set; }
public int SomeInteger { get; set; }
public bool SomeBoolean { get; set; }
string m_strSomeStringDeserializeOnly = "This is serialized";
public string SomeStringReadOnly
{
get { return m_strSomeStringDeserializeOnly; }
set { SomeString = value; }
}
public override string ToString()
{
return String.Format("{0}({3}), {1}, {2}", SomeString, SomeInteger, SomeBoolean, SomeStringReadOnly);
}
}
}
So if you serialize a SampleClass-object, SomeString is ignored, but SomeStringDeserializeOnly is written. If you deserialize a "SampleClass"-object, the value of SomeStringDeserializeOnly is read and set to the SomeString property (done in the set-accessor of SomeStringDeserializeOnly property).
I hope this is useful to you, but what puzzles me is why you want to do that? I can look back to "some years" of professional coding and countless serialization scenarios, but I never came across such a requirenment for non fine tuned serialization - You have to write another value anyway if you want to read something back... Are you shure this is what you need? (sorry if I completly missunderstood you, but I'd have done it like this - use a normal property and serialize/deserialize - and just assign the "non written/but read" property (use XMLIgnore on that) after deserialization).