C# 从对象创建Xml文件
我是一名网页设计师的网页开发人员,我们通常这样做: -我创建了系统,生成了一些Xml文件 -设计器使用xslt显示xml文件 没什么新鲜事 我的问题是我使用Xml序列化来创建Xml文件,但我从不使用反序列化。所以我想知道是否有一种方法可以避免这样的修复:C# 从对象创建Xml文件,c#,.net,xml-serialization,C#,.net,Xml Serialization,我是一名网页设计师的网页开发人员,我们通常这样做: -我创建了系统,生成了一些Xml文件 -设计器使用xslt显示xml文件 没什么新鲜事 我的问题是我使用Xml序列化来创建Xml文件,但我从不使用反序列化。所以我想知道是否有一种方法可以避免这样的修复: 我的属性的空setter 空无参数构造函数 实现IXmlSerializable并在反序列化时抛出“notimplementedexception” 使用公共字段复制该类 好的,第一次误读你的问题!我很确定这是无法避免的。必须有一个无参数构造
- 我的属性的空setter
- 空无参数构造函数
- 实现IXmlSerializable并在反序列化时抛出“notimplementedexception”
- 使用公共字段复制该类
- 好的,第一次误读你的问题!我很确定这是无法避免的。必须有一个无参数构造函数,并且不能序列化只读属性。我想你唯一的选择是。好吧,第一次看错了你的问题!我很确定这是无法避免的。必须有一个无参数构造函数,并且不能序列化只读属性。我想你唯一的选择是。
本文描述了如何创建一个自定义XML序列化程序,以便可以序列化私有字段-可能需要对所需的表单进行一些造型,但这比看起来更简单(老实说!)这是编写您自己的序列化程序/反序列化程序的一个良好开端,它将完全序列化您想要的内容,并且不关心无参数构造函数或可写属性
我能想到的唯一其他解决方案是为每个可序列化类创建一个包装器类——但我不知道从长远来看这会有多好。我只是觉得不好。
本文描述了如何创建一个自定义XML序列化程序,以便可以序列化私有字段-可能需要对所需的表单进行一些造型,但这比看起来更简单(老实说!)这是编写您自己的序列化程序/反序列化程序的一个良好开端,它将完全序列化您想要的内容,并且不关心无参数构造函数或可写属性
我能想到的唯一其他解决方案是为每个可序列化类创建一个包装器类——但我不知道从长远来看这会有多好。我只是觉得不好。好吧,现在我明白了。我认为用XMLSerialization无法实现这一点。
XMLSerialization需要这些信息来重新填充对象。它不知道某些用户从不反序列化数据。您可能需要编写一些代码来为对象生成XML。好的,现在我明白了。我认为用XMLSerialization无法实现这一点。
XMLSerialization需要这些信息来重新填充对象。它不知道某些用户从不反序列化数据。您可能需要编写一些代码来为对象生成XML。我知道您不想添加无参数构造函数或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
{
}
}
}
}
namespace Aesop.Dto
{
使用制度;
使用System.Xml.Serialization;
///
///表示组织ID/名称的组合。
///
[可序列化]
公共密封类组织
{
///
///该组织的名称。
///
私有只读字符串名称;
///
///组织的ID。
///
私有只读int-id;
///
///初始化类的新实例。
///
///该组织的名称。
///组织的ID。
公共组织(字符串名称,int-id)
{
this.name=名称;
this.id=id;
}
///
///防止类的默认实例被删除
///正在创建。
///
私有组织()
{
}
///
///获取或设置组织的名称。
///
///该组织的名称。
[XmlAttribute]
公共字符串名
{
得到
{
返回此.name;
}
设置
{
}
}
///
///获取或设置组织的ID。
///
///组织的ID。
[XmlAttribute]
公共整数ID
{
得到
{
返回此.id;
}
设置
{
}
}
}
}
我知道您不想添加无参数构造函数或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
{
}
}
}
}
namespace Aesop.Dto
{
使用制度;
使用System.Xml.Serialization;
///
///表示组织ID/名称的组合。
///
[可序列化]
公共密封类组织
{
///
///该组织的名称。
///
私有只读字符串名称;
///
///组织的ID。
///
私有只读int-id;
///
///初始化类的新实例。
///
///该组织的名称。
///组织的ID。
公共组织(字符串名称,int-id)
{
this.name=名称;
this.id=id;
}
///
///防止类的默认实例被删除
///正在创建。
///
私有组织()
{
}
///
///获取或设置组织的名称。
///
///该组织的名称。
[XmlAttribute]
公共字符串名
{
得到
{
返回此.name;
}
设置
{
}
}
/