【问题标题】:XML File Contains Only Blank Element Called ObjectProxy - C# XML SerializationXML 文件仅包含名为 ObjectProxy 的空白元素 - C# XML 序列化
【发布时间】:2017-06-20 20:30:33
【问题描述】:

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

我使用了以下帮助类来实现这一点:

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;
        }
    }

在我的方法中,在我创建/删除/更改对象的任何地方,我都使用如下代码来序列化对象数据:

        //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"/>

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

任何帮助将不胜感激!

【问题讨论】:

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

标签: c# xml serialization xml-serialization


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    相关资源
    最近更新 更多