从对象创建Xml文件

时间:2010-04-07 13:47:19

标签: c# .net xml-serialization

我是一名网络开发人员,我是一名网页设计师,我们通常这样做: - 我创建了系统,我生成了一些Xml文件 - 设计器使用xslt

显示xml文件

没什么新的。

我的问题是我使用Xml序列化来创建我的xml文件,但我从不使用反序列化。所以我想知道是否有办法避免像这样修复:

  • 我的属性的空setter

  • 空参数构造函数

  • 实现IXmlSerializable并在反序列化时抛出“notimplementedexception”

  • 使用公共字段

  • 复制该课程

5 个答案:

答案 0 :(得分:4)

好的第一次误读你的问题!很确定没有办法避免这种情况。 是无参数构造函数,您无法序列化只读属性。我认为你唯一的另一个选择是DataContractSerializer

答案 1 :(得分:1)

http://blogs.mastronardi.be/Sandro/2007/08/22/CustomXMLSerializerBasedOnReflectionForSerializingPrivateVariables.aspx

本文介绍如何创建自定义XML序列化程序,以便您可以序列化私有字段 - 可能需要对您想要的表单进行一些成型,但它比它看起来更容易(诚实!)并且这是一个良好的开始写作您自己的序列化器/反序列化器,它将准确地序列化您想要的 - 并且不关心无参数构造函数或可写属性。

我能想到的唯一其他解决方案是为每个可序列化的类创建一个包装类 - 但我不知道从长远来看会有多好。我只是觉得它不好。

答案 2 :(得分:0)

class Preferences
{
    private const string filePreferences = "preferences.xml";

    public Preferences() { }

    public static Preferences Load()
    {
        Preferences pref = null;
        if (File.Exists(Preferences.FileFullPath))
        {
            var serializer = new System.Xml.Serialization.XmlSerializer(typeof(Preferences));
            using (var xmlReader = new System.Xml.XmlTextReader(Preferences.FileFullPath))
            {
                if (serializer.CanDeserialize(xmlReader))
                {
                    pref = serializer.Deserialize(xmlReader) as Preferences;
                }
            }
        }
        return ((pref == null) ? new Preferences() : pref);
    }

    public void Save()
    {
        var preferencesFile = FileFullPath;
        var preferencesFolder = Directory.GetParent(preferencesFile).FullName;

        using (var fileStream = new FileStream(preferencesFile, FileMode.Create))
        {
            new System.Xml.Serialization.XmlSerializer(typeof(Preferences)).Serialize(fileStream, this);
        }
    }
}

答案 3 :(得分:0)

好的,现在我明白了。我不认为有任何方法可以使用XMLSerialization。 XMLSerialization需要这些信息来重新填充对象。它不知道某些用户从不反序列化数据。您可能必须编写一些代码来为对象生成XML。

答案 4 :(得分:0)

我知道你不想添加无参数构造函数或setter,但这是使用XmlSerializer的唯一方法。好消息是无参数构造函数可以是私有的,而setter 可以是空的,序列化将起作用。见:

namespace Aesop.Dto
{
    using System;
    using System.Xml.Serialization;

    /// <summary>
    /// Represents an Organization ID/Name combination.
    /// </summary>
    [Serializable]
    public sealed class Org
    {
        /// <summary>
        /// The organization's name.
        /// </summary>
        private readonly string name;

        /// <summary>
        /// The organization's ID.
        /// </summary>
        private readonly int id;

        /// <summary>
        /// Initializes a new instance of the <see cref="Org"/> class.
        /// </summary>
        /// <param name="name">The organization's name.</param>
        /// <param name="id">The organization's ID.</param>
        public Org(string name, int id)
        {
            this.name = name;
            this.id = id;
        }

        /// <summary>
        /// Prevents a default instance of the <see cref="Org"/> class from
        /// being created.
        /// </summary>
        private Org()
        {
        }

        /// <summary>
        /// Gets or sets the organization's name.
        /// </summary>
        /// <value>The organization's name.</value>
        [XmlAttribute]
        public string Name
        {
            get
            {
                return this.name;
            }

            set
            {
            }
        }

        /// <summary>
        /// Gets or sets the organization's ID.
        /// </summary>
        /// <value>The organization's ID.</value>
        [XmlAttribute]
        public int ID
        {
            get
            {
                return this.id;
            }

            set
            {
            }
        }
    }
}
相关问题