XML 文件仅包含名为 ObjectProxy 的空白元素 - C# XML 序列化

XML File Contains Only Blank Element Called ObjectProxy - C# XML Serialization

我已将 XML 序列化添加到我的项目中,以便将我的对象数据存储在 XML 文件中。

我使用了以下助手 class 来实现这一点:

public static class SerializerHelper
    {
        /// <summary>
        /// Serializes an object.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="serializableObject"></param>
        /// <param name="fileName"></param>

        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(
            "SerializerHelper.cs");

        public static void SerializeObject<T>(string filepath, T serializableObject)
        {
            if (serializableObject == null) { return; }

            try
            {
                XmlDocument xmlDocument = new XmlDocument();
                XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
                using (MemoryStream stream = new MemoryStream())
                {
                    serializer.Serialize(stream, serializableObject);
                    stream.Position = 0;
                    xmlDocument.Load(stream);
                    xmlDocument.Save(filepath);
                    stream.Close();
                }
            }
            catch (Exception ex)
            {
                //Log exception here
                logger.Error("Error Serializing: " + ex.Message);
            }
        }


        /// <summary>
        /// Deserializes an xml file into an object list
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static T DeSerializeObject<T>(string filepath)
        {
            T objectOut = default(T);

            if (!System.IO.File.Exists(filepath)) return objectOut;

            try
            {
                string attributeXml = string.Empty;

                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(filepath);
                string xmlString = xmlDocument.OuterXml;

                using (StringReader read = new StringReader(xmlString))
                {
                    Type outType = typeof(T);

                    XmlSerializer serializer = new XmlSerializer(outType);
                    using (XmlReader reader = new XmlTextReader(read))
                    {
                        objectOut = (T)serializer.Deserialize(reader);
                        reader.Close();
                    }

                    read.Close();
                }
            }
            catch (Exception ex)
            {
                //Log exception here
                logger.Error("Error Deserializing: " + ex.Message);
            }

            return objectOut;
        }
    }

在我的方法中,在我 create/delete/alter 对象的任何地方,我使用如下代码序列化对象数据:

        //Increments the failed logon attempts counter
        public void incrementFailedLogonAttempts()
        {
            logonAttemptCounter.incrementFailedLogons();

            //Update the failed logon attempt counter in the XML Data Store
            SerializerHelper.SerializeObject(@"C:\Users\Michael"
            + @"\Google Drive\FDM Dev Course Content\Workspace\SystemAdmin\SystemAdmin\"
            + @"XML Data Store\LogonAttemptCounter.xml", logonAttemptCounter);
        }

然而,在 运行 我所有的单元测试之后,我的一些 XML 文件(在 运行 所有测试之前序列化良好)现在看起来像这样:

<?xml version="1.0"?>
<ObjectProxy_4 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

有谁知道这里可能出了什么问题?或者为什么会出错?

如有任何帮助,我们将不胜感激!

我想我其实知道。我的测试使用模拟对象来模拟依赖项。在我的许多测试中,我创建了要测试其方法的对象,在构造函数中注入模拟对象。这是导致问题的原因吗?我正在序列化根据定义为空的模拟对象?