XmlSerializer 保存空文件

发布于 2024-12-08 03:10:14 字数 1851 浏览 3 评论 0原文

我在序列化对象时遇到问题。 我实现了 IXmlSerialized 接口并初始化 XmlSerializer 对象(例如序列化器)。 但有时在调用serializer.Serialize(writer, data)后,我的输出文件看起来像这样: 在此处输入图像描述

为什么我要这样做有这样的行为吗?

public class MyClass : IData
{
        private static readonly XmlSerializer _formatter = new XmlSerializer(typeof(MyData));

    public void Save(string filePath)
        {
            using (StreamWriter writer = new StreamWriter(filePath))
            {
                Save(writer);
                writer.Close();
            }
        }


    public void Save(TextWriter Writer)
        {
            MyData data = GetMyDataObject();

            _formatter.Serialize(Writer, data);
        }

    private MyData GetMyDataObject()
        {
            MyData data = new MyData ();
            foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(typeof(IData)))
                pd.SetValue(data, pd.GetValue(this));
            return data;
        }
}


    public class MyData : IData, IXmlSerializable
{

    public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteAttributeString("Number", Number);
            if (HardwareInformation != null)
            {
                writer.WriteStartElement("HWInfoList");
                foreach (KeyValuePair<string, string> kw in HardwareInformation)
                {
                    writer.WriteStartElement("HWInfo");
                    writer.WriteAttributeString("Key", kw.Key);
                    writer.WriteAttributeString("Value", kw.Value);
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();
            }
        }
}
public interface IData
{

Dictionary<string, string> HardwareInformation { get; set; }
string Number { get; set; }
}

I have a problem with serializing my objects.
I implemented IXmlSerializable interface and initialize object of XmlSerializer(for example, serializer).
But sometimes after calling serializer.Serialize(writer, data) my output file looks like this: enter image description here

why do I have such behavior?

public class MyClass : IData
{
        private static readonly XmlSerializer _formatter = new XmlSerializer(typeof(MyData));

    public void Save(string filePath)
        {
            using (StreamWriter writer = new StreamWriter(filePath))
            {
                Save(writer);
                writer.Close();
            }
        }


    public void Save(TextWriter Writer)
        {
            MyData data = GetMyDataObject();

            _formatter.Serialize(Writer, data);
        }

    private MyData GetMyDataObject()
        {
            MyData data = new MyData ();
            foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(typeof(IData)))
                pd.SetValue(data, pd.GetValue(this));
            return data;
        }
}


    public class MyData : IData, IXmlSerializable
{

    public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteAttributeString("Number", Number);
            if (HardwareInformation != null)
            {
                writer.WriteStartElement("HWInfoList");
                foreach (KeyValuePair<string, string> kw in HardwareInformation)
                {
                    writer.WriteStartElement("HWInfo");
                    writer.WriteAttributeString("Key", kw.Key);
                    writer.WriteAttributeString("Value", kw.Value);
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();
            }
        }
}
public interface IData
{

Dictionary<string, string> HardwareInformation { get; set; }
string Number { get; set; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为人所爱 2024-12-15 03:10:14

你如何进行序列化?这是序列化函数的示例

   private XDocument Serialize<T>(object obj)
    {
        XDocument ReturnValue = new XDocument();

        XmlSerializer Serializer = new XmlSerializer(typeof(T));
        System.IO.StringWriter sw = new System.IO.StringWriter();        
        System.IO.StringReader sr; 
        Serializer.Serialize(sw, obj);
        sr = new System.IO.StringReader(sw.ToString());

        ReturnValue = XDocument.Load(sr);

        return ReturnValue;
    }

How are you serializing? Here is an example of a Serialize Function

   private XDocument Serialize<T>(object obj)
    {
        XDocument ReturnValue = new XDocument();

        XmlSerializer Serializer = new XmlSerializer(typeof(T));
        System.IO.StringWriter sw = new System.IO.StringWriter();        
        System.IO.StringReader sr; 
        Serializer.Serialize(sw, obj);
        sr = new System.IO.StringReader(sw.ToString());

        ReturnValue = XDocument.Load(sr);

        return ReturnValue;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文