0

可能重复:
使用 XmlSerializer 将空 xml 属性值反序列化为可为空的 int 属性

我想将以下 XML 反序列化为 C# 对象。它包含具有整数类型值或空值的元素:CustomerID=”1005” 或 CustomerID=””。关于如何使用可为空的 int 将元素值转换为属性的任何想法。

<ExportDefinition>
  <DestinationDir>C:\Test\</DestinationDir>
  <CustomerID>1005</CustomerID>
</ExportDefinition>
public ExportDefinition GetExportDefinition(FileInfo exportDefFile)
        {
            if (!exportDefFile.Exists)
                throw new Exception(string.Format("Export definition file '{0}' does not exists.", exportDefFile.FullName));

            FileStream readFileStream = null;
            XmlSerializer serializerObj = null;
            ExportDefinition loadedObj = null;

            try
            {
                readFileStream = new FileStream(exportDefFile.FullName, FileMode.Open, FileAccess.Read, FileShare.Read);

                serializerObj = new XmlSerializer(typeof(ExportDefinition));

                loadedObj = (ExportDefinition)serializerObj.Deserialize(readFileStream);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (readFileStream != null)
                    readFileStream.Close();
            }
            return loadedObj;
        }
4

0 回答 0