前段时间应bestcomy兄弟的,提议,我对nGallery一个开源的WEB电子相册项目(见http://www.ngallery.org)做了部分本地化的工作.
当时纯属偶然我修改了它的数据配置文件ngallery.config把
<?xml version="1.0"?>
<Configuration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
......
.....
</Configuration>
我把上面改为
<Configuration1 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
......
.....
</Configuration1>
结果出在类Configuration中出现异常
using System.Web;
using System.IO;
using System.Xml.Serialization;
namespace nGallery.Lib
{
RewriteType Enum#region RewriteType Enum
public enum RewriteType
{
ById,
ByName
}
#endregion
/**//// <summary>
/// <c>Configuration</c> provides an object that contains all
/// the application configuration settings.
/// </summary>
[Serializable]
public class Configuration
{
Private Members#region Private Members
private static string _configurationFilePath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory.Replace("/",System.IO.Path.DirectorySeparatorChar.ToString()),"data" + System.IO.Path.DirectorySeparatorChar + "ngallery.config");
private static Configuration _currentConfiguration;
private string _siteTitle;
private string _siteSkin;
private bool _enableComments;
private bool _enableRatings;
private bool _enableExif;
private bool _enableStats;
private string _sitePassword;
private string _datastoreType;
private string _connectionString;
private string _adminUserName;
private string _adminPassword;
private string _adminFullName;
private string _adminEmailAddress;
private string _smtpServer;
private string _commentType;
private int _albumListImageHeight;
private int _albumListImageWidth;
private int _albumListImageQuality;
private int _albumListRecordsPerPage;
private int _albumListDescLength;
private string _albumListPagingDisplayType;
private bool _originalPictureWatermark;
private int _pictureListImageHeight;
private int _pictureListImageWidth;
private bool _pictureListMaintainRatio;
private int _pictureListImageQuality;
private int _pictureDetailsImageHeight;
private int _pictureDetailsImageWidth;
private bool _pictureDetailsMaintainRatio;
private int _pictureDetailsImageQuality;
private bool _pictureDetailsWatermark;
private int _pictureListRecordsPerPage;
private int _pictureListColumnsPerPage;
private string _pictureListPagingDisplayType;
private int _pictureMaxHeight;
private int _pictureMaxWidth;
private string _adminDirectory;
private string _dataDirectory;
private string _templateDirectory;
private string _photosDirectory;
private bool _customErrorPageEnabled;
private int _slideShowDelay;
private int _slideShowImageHeight;
private int _slideShowImageWidth;
private int _slideShowImageQuality;
private bool _slideShowWatermark;
private int _summaryImageHeight;
private int _summaryImageWidth;
private bool _summaryMaintainRatio;
private int _summaryImageQuality;
private bool _summaryWatermark;
private string _watermarkImage;
private string _watermarkText;
private Definitions.WatermarkType _watermarkType;
private Definitions.WatermarkPosition _watermarkPosition;
private string _characterSet;
private string _exifProperties;
private RewriteType _rewriteType;
#endregion
Constructor#region Constructor
public Configuration()
{
}
#endregion
Public Properties#region Public Properties
public RewriteType AlbumRewriting
{
get { return _rewriteType; }
set { _rewriteType = value; }
}
public string CharacterSet
{
get { return _characterSet; }
set { _characterSet = value; }
}
public string SiteTitle
{
get { return _siteTitle; }
set { _siteTitle = value; }
}
public string SiteSkin
{
get { return _siteSkin; }
set { _siteSkin = value; }
}
public bool EnableComments
{
get { return _enableComments; }
set { _enableComments = value; }
}
public bool EnableRatings
{
get { return _enableRatings; }
set { _enableRatings = value; }
}
public bool EnableExif
{
get { return _enableExif; }
set { _enableExif = value; }
}
public bool EnableStats
{
get { return _enableStats; }
set { _enableStats = value; }
}
public string SitePassword
{
get { return _sitePassword; }
set { _sitePassword = value; }
}
public string DatastoreType
{
get { return _datastoreType; }
set { _datastoreType = value; }
}
public string ConnectionString
{
get { return _connectionString; }
set { _connectionString = value; }
}
public string AdminUserName
{
get { return _adminUserName; }
set { _adminUserName = value; }
}
public string AdminPassword
{
get { return _adminPassword; }
set { _adminPassword = value; }
}
public string AdminFullName
{
get { return _adminFullName; }
set { _adminFullName = value; }
}
public string AdminEmailAddress
{
get { return _adminEmailAddress; }
set { _adminEmailAddress = value; }
}
public string SmtpServer
{
get { return _smtpServer; }
set { _smtpServer = value; }
}
public string CommentType
{
get { return _commentType; }
set { _commentType = value; }
}
public int AlbumListImageHeight
{
get { return _albumListImageHeight; }
set { _albumListImageHeight = value; }
}
public int AlbumListImageWidth
{
get { return _albumListImageWidth; }
set { _albumListImageWidth = value; }
}
public int AlbumListImageQuality
{
get { return _albumListImageQuality; }
set { if((value > 0) && (value <= 100)) _albumListImageQuality = value; }
}
public int AlbumListRecordsPerPage
{
get { return _albumListRecordsPerPage; }
set { _albumListRecordsPerPage = value; }
}
public int AlbumListDescLength
{
get { return _albumListDescLength; }
set { _albumListDescLength = value; }
}
public string AlbumListPagingDisplayType
{
get { return _albumListPagingDisplayType; }
set { _albumListPagingDisplayType = value; }
}
public bool OriginalPictureWatermark
{
get { return _originalPictureWatermark; }
set { _originalPictureWatermark = value; }
}
public int PictureListImageHeight
{
get { return _pictureListImageHeight; }
set { _pictureListImageHeight = value; }
}
public int PictureListImageWidth
{
get { return _pictureListImageWidth; }
set { _pictureListImageWidth = value; }
}
public bool PictureListMaintainRatio
{
get { return _pictureListMaintainRatio; }
set { _pictureListMaintainRatio = value; }
}
public int PictureListImageQuality
{
get { return _pictureListImageQuality; }
set { if((value > 0) && (value <= 100)) _pictureListImageQuality = value; }
}
public int PictureDetailsImageHeight
{
get { return _pictureDetailsImageHeight; }
set { _pictureDetailsImageHeight = value; }
}
public int PictureDetailsImageWidth
{
get { return _pictureDetailsImageWidth; }
set { _pictureDetailsImageWidth = value; }
}
public bool PictureDetailsMaintainRatio
{
get { return _pictureDetailsMaintainRatio; }
set { _pictureDetailsMaintainRatio = value; }
}
public int PictureDetailsImageQuality
{
get { return _pictureDetailsImageQuality; }
set { if((value > 0) && (value <= 100)) _pictureDetailsImageQuality = value; }
}
public bool PictureDetailsWatermark
{
get { return _pictureDetailsWatermark; }
set { _pictureDetailsWatermark = value; }
}
public int PictureListRecordsPerPage
{
get { return _pictureListRecordsPerPage; }
set { _pictureListRecordsPerPage = value; }
}
public int PictureListColumnsPerPage
{
get { return _pictureListColumnsPerPage; }
set { _pictureListColumnsPerPage = value; }
}
public string PictureListPagingDisplayType
{
get { return _pictureListPagingDisplayType; }
set { _pictureListPagingDisplayType = value; }
}
public int PictureMaxHeight
{
get { return _pictureMaxHeight; }
set { _pictureMaxHeight = value; }
}
public int PictureMaxWidth
{
get { return _pictureMaxWidth; }
set { _pictureMaxWidth = value; }
}
public string AdminDirectory
{
get { return _adminDirectory; }
set { _adminDirectory = value; }
}
public string DataDirectory
{
get { return _dataDirectory; }
set { _dataDirectory = value; }
}
public string TemplateDirectory
{
get { return _templateDirectory; }
set { _templateDirectory = value; }
}
public string PhotosDirectory
{
get { return _photosDirectory; }
set { _photosDirectory = value; }
}
public bool CustomErrorPageEnabled
{
get { return _customErrorPageEnabled; }
set { _customErrorPageEnabled = value; }
}
public int SlideShowDelay
{
get { return _slideShowDelay; }
set { _slideShowDelay = value; }
}
public int SlideShowImageHeight
{
get { return _slideShowImageHeight; }
set { _slideShowImageHeight = value; }
}
public int SlideShowImageWidth
{
get { return _slideShowImageWidth; }
set { _slideShowImageWidth = value; }
}
public int SlideShowImageQuality
{
get { return _slideShowImageQuality; }
set { if((value > 0) && (value <= 100)) _slideShowImageQuality = value; }
}
public bool SlideShowWatermark
{
get { return _slideShowWatermark; }
set { _slideShowWatermark = value; }
}
public int SummaryImageHeight
{
get { return _summaryImageHeight; }
set { _summaryImageHeight = value; }
}
public int SummaryImageWidth
{
get { return _summaryImageWidth; }
set { _summaryImageWidth = value; }
}
public bool SummaryMaintainRatio
{
get { return _summaryMaintainRatio; }
set { _summaryMaintainRatio = value; }
}
public int SummaryImageQuality
{
get { return _summaryImageQuality; }
set { if((value > 0) && (value <= 100)) _summaryImageQuality = value; }
}
public bool SummaryWatermark
{
get { return _summaryWatermark; }
set { _summaryWatermark = value; }
}
public string WatermarkImage
{
get { return _watermarkImage; }
set { _watermarkImage = value; }
}
public string WatermarkText
{
get { return _watermarkText; }
set { _watermarkText = value; }
}
public Definitions.WatermarkType WatermarkType
{
get { return _watermarkType; }
set { _watermarkType = value; }
}
public Definitions.WatermarkPosition WatermarkPosition
{
get { return _watermarkPosition; }
set { _watermarkPosition = value; }
}
public string EXIFProperties
{
get {return _exifProperties;}
set {_exifProperties = value;}
}
#endregion
Static Methods#region Static Methods
/**//// <summary>
/// return a static Configuration object
/// </summary>
/// <returns></returns>
public static Configuration Instance()
{
if (_currentConfiguration == null)
{
Configuration.Load();
if (_currentConfiguration == null)
_currentConfiguration = new Configuration();
}
return _currentConfiguration;
}
public static void Load()
{
if (!File.Exists(_configurationFilePath))
{
_currentConfiguration = new Configuration();
}
else
{
XmlSerializer ser = new XmlSerializer(typeof(Configuration));
//use uft-8 to read stream
StreamReader reader = new StreamReader(_configurationFilePath);
// Deserialize the object into a AlbumCollection and return it.
_currentConfiguration = (Configuration) ser.Deserialize(reader);
reader.Close();
}
}
public static void Load(string configFile)
{
_configurationFilePath = configFile;
Load();
}
public static void Save()
{
XmlSerializer ser = new XmlSerializer(typeof(Configuration));
FileStream fileOut;
if (!File.Exists(_configurationFilePath))
{
fileOut = new FileStream(_configurationFilePath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
}
else
{
fileOut = new FileStream(_configurationFilePath, FileMode.Truncate, FileAccess.ReadWrite, FileShare.ReadWrite);
}
// Serialize the object in XML to the given file stream.
ser.Serialize(fileOut, _currentConfiguration);
// Close the file stream.
fileOut.Close();
}
#endregion
}
}
显然出现异常的代码是Load()方法里的这一句
_currentConfiguration = (Configuration) ser.Deserialize(reader);显然是因为没有 [XmlRoot("Configuration", Namespace=nGrallery.Lib)]
于是我上面类Configuration里的几个属性改为只读(去掉set属性),结果反序列化可以成功,查看得知那几个被我修改为只读属性的字段值为null。还不死心,我把配置文件ngallery.config里部分结点给去掉,反序列化还是可以成功。。。。
那可想,.net框架底层在处理反序列化时在内存中的映射是通过命名空间(或[XmlRoot(name)]/[XmlElement(name)]/[XmlAttribute("name")..])来映射的,至于对配置节到类属性字段的映射则是匹配不成功就跳过呢?
如果这样的话,那些没有加载到内存中的XML数据很多将会没有什么意义,有没有好的算法来解决这个问题呢?期待中...
有点迷惑