Class 到 XML 的序列化会留下空白值
Serialisation of Class to XML is leaving Blank Values
很难解释,任何人都可以找出问题所在吗?
我正在将 class 序列化为 XML,但是其中一个属性(Class 中的 public Class)被赋予空白XML.
中的值
我正在创建自己的设置 Class,我正在将其序列化为 XML 并反序列化回应用程序的 class 加载。
我已将调试点放入 Save 方法中,"ColourScheme" 确实有值,但它们在 XML 中被屏蔽了。
public class SettingsModel
{
// DECLARE: Settings File Location
private static string SettingsFilePath = String.Format(@"{0}\{1}\{2}", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Typhoeus BluePrint", "BluePrint_Settings.xml");
// DECLARE: Public Settings
public DateTime LastUpdated = System.DateTime.Now;
public string StylesheetName = "style.css";
public string WebPageName = "index.html";
public string ProjectDirectory = String.Format(@"{0}\{1}\{2}", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Typhoeus BluePrint", "Projects");
public ColourSchemeClass ColourScheme = new ColourSchemeClass();
public SettingsModel()
{
}
public void Save()
{
LastUpdated = System.DateTime.Now;
// DECLARE: File Stream Object
FileStream fs = new FileStream(SettingsFilePath, FileMode.OpenOrCreate);
// DECLARE: Serialisation Parameters
XmlSerializer ClassSerialiser = new XmlSerializer(typeof(SettingsModel));
// SERIALISE: This Class to XML
ClassSerialiser.Serialize(fs, this);
// CLOSE: File Stream
fs.Close();
}
}
这是配色方案Class
public class ColourSchemeClass
{
#region Objects
// DECLARE: Colour Scheme Properties
public Color BaseColour = ColorTranslator.FromHtml("#1E1E1E");
public Color HighlightColour = ColorTranslator.FromHtml("#414141");
public Color SelectedColour = Color.White;
#endregion
#region Constructors
public ColourSchemeClass()
{
}
public ColourSchemeClass (Color Base, Color Highlight, Color Selected)
{
BaseColour = Base;
HighlightColour = Highlight;
SelectedColour = Selected;
}
#endregion
}
这是连载后的XML。 (摘录因为我这里好像没法格式化)(替换标签用方括号打开和关闭)
[ColourScheme]
[BaseColour/]
[HighlightColour/]
[SelectedColour/]
[/ColourScheme]
Color
is immutable (no setters) so it can't be usefully serialized by XmlSerializer
。你需要为每个颜色属性引入一个代理属性,例如:
[XmlIgnore]
public Color BaseColour { get; set; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
[XmlElement("BaseColour")]
public string XmlBaseColour
{
get
{
return ColorTranslator.ToHtml(BaseColour);
}
set
{
BaseColour = ColorTranslator.FromHtml(value);
}
}
很难解释,任何人都可以找出问题所在吗?
我正在将 class 序列化为 XML,但是其中一个属性(Class 中的 public Class)被赋予空白XML.
中的值我正在创建自己的设置 Class,我正在将其序列化为 XML 并反序列化回应用程序的 class 加载。
我已将调试点放入 Save 方法中,"ColourScheme" 确实有值,但它们在 XML 中被屏蔽了。
public class SettingsModel
{
// DECLARE: Settings File Location
private static string SettingsFilePath = String.Format(@"{0}\{1}\{2}", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Typhoeus BluePrint", "BluePrint_Settings.xml");
// DECLARE: Public Settings
public DateTime LastUpdated = System.DateTime.Now;
public string StylesheetName = "style.css";
public string WebPageName = "index.html";
public string ProjectDirectory = String.Format(@"{0}\{1}\{2}", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Typhoeus BluePrint", "Projects");
public ColourSchemeClass ColourScheme = new ColourSchemeClass();
public SettingsModel()
{
}
public void Save()
{
LastUpdated = System.DateTime.Now;
// DECLARE: File Stream Object
FileStream fs = new FileStream(SettingsFilePath, FileMode.OpenOrCreate);
// DECLARE: Serialisation Parameters
XmlSerializer ClassSerialiser = new XmlSerializer(typeof(SettingsModel));
// SERIALISE: This Class to XML
ClassSerialiser.Serialize(fs, this);
// CLOSE: File Stream
fs.Close();
}
}
这是配色方案Class
public class ColourSchemeClass
{
#region Objects
// DECLARE: Colour Scheme Properties
public Color BaseColour = ColorTranslator.FromHtml("#1E1E1E");
public Color HighlightColour = ColorTranslator.FromHtml("#414141");
public Color SelectedColour = Color.White;
#endregion
#region Constructors
public ColourSchemeClass()
{
}
public ColourSchemeClass (Color Base, Color Highlight, Color Selected)
{
BaseColour = Base;
HighlightColour = Highlight;
SelectedColour = Selected;
}
#endregion
}
这是连载后的XML。 (摘录因为我这里好像没法格式化)(替换标签用方括号打开和关闭)
[ColourScheme]
[BaseColour/]
[HighlightColour/]
[SelectedColour/]
[/ColourScheme]
Color
is immutable (no setters) so it can't be usefully serialized by XmlSerializer
。你需要为每个颜色属性引入一个代理属性,例如:
[XmlIgnore]
public Color BaseColour { get; set; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
[XmlElement("BaseColour")]
public string XmlBaseColour
{
get
{
return ColorTranslator.ToHtml(BaseColour);
}
set
{
BaseColour = ColorTranslator.FromHtml(value);
}
}