另一个“保留参考”是XmlSerializer问题
我正在研究一种“新语言”(不是那么雄心勃勃)的 XML 定义,我希望能够选择同时通过 xml(序列化/反序列化)和 API 处理对象图。
public class Project
{
public List<Connection> Connections { get; set; }
public List<Table> Tables { get; set; }
/* Constructors and more.... */
}
public class Connection
{
public string Name { get; set; }
public string ConnectionString { get; set; }
/* Constructors and more.... */
}
public class Table
{
public string TableName { get; set; }
public Connection Conn { get; set; }
/* Constructors and more.... */
}
好的,现在我想用类似的方法序列化/反序列化它:
<Project>
<Connections>
<Connection Name="MyConnName" ConnectionString="My connection string"\>
<\Connections>
<Tables>
<Table TableName="MyTable" ConnectionName="MyConnName"\>
<\Tables>
<\Project>
这里有两个问题:
该类有一个“Conn”属性,它是对连接类的引用,但在“语言”中(Xml 序列化)被重命名为“ConnectionName”(我想更改名称,以避免纯对象引用(类)和语言“按名称引用”(Xml 序列化)之间的混淆
如您所见,我想保留引用,但不包括“z.id ??”,就像当preserveObjectReference设置为true时DataContractSerializer所做的那样,而是我想使用“名称”(更易于人类阅读)
有什么想法吗?
I´m working on a "new language" (not such ambitious) XML definition, I want to have the option to work with object graph vía xml (serializing/deserializing) and API at same time.
public class Project
{
public List<Connection> Connections { get; set; }
public List<Table> Tables { get; set; }
/* Constructors and more.... */
}
public class Connection
{
public string Name { get; set; }
public string ConnectionString { get; set; }
/* Constructors and more.... */
}
public class Table
{
public string TableName { get; set; }
public Connection Conn { get; set; }
/* Constructors and more.... */
}
OK, now I want to serialize/deserialize this with something like:
<Project>
<Connections>
<Connection Name="MyConnName" ConnectionString="My connection string"\>
<\Connections>
<Tables>
<Table TableName="MyTable" ConnectionName="MyConnName"\>
<\Tables>
<\Project>
There are two issues here:
The class has a "Conn" property that is a reference to a Connection Class, but in the "language" (Xml serialization) is renamed to "ConnectionName" (I want to change the name avoiding confusion between pure Object reference (Class) and language "reference by name" (Xml seralization)
As you can see, I want to preserve reference, but no including "z.id ??" like DataContractSerializer does when preserveObjectReference is set to true, instead I want to use "names" (much more human readable)
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于“您想要的”的几乎任何有用值,确实没有什么好的方法可以扩展 XmlSerializer 来执行您想要的操作。
要生成您正在寻找的 XML 类型,您必须使用
[XmlIgnore]
修饰Connection
属性,添加ConnectionName
供 XmlSerializer 使用的属性,并在设置ConnectionName
时或之后的某个时间找到适当的Connection
。或者,您需要让
Table
实现IXmlSerialized
并完全手动实现生成< /代码> 元素。
There's really no good way to extend
XmlSerializer
to do what you want, for almost any useful value of 'what you want'.To generate the sort of XML you're looking for, you'll have to decorate the
Connection
property with[XmlIgnore]
, add aConnectionName
property for the XmlSerializer to use, and locate the appropriateConnection
either whenConnectionName
is set or sometime after.Alternatively, you'll need to have
Table
implementIXmlSerializable
and completely hand-implement the code that generates the<Table>
element.