另一个“保留参考”是XmlSerializer问题

发布于 2024-12-04 03:48:45 字数 1174 浏览 1 评论 0原文

我正在研究一种“新语言”(不是那么雄心勃勃)的 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>

这里有两个问题:

  1. 该类有一个“Conn”属性,它是对连接类的引用,但在“语言”中(Xml 序列化)被重命名为“ConnectionName”(我想更改名称,以避免纯对象引用(类)和语言“按名称引用”(Xml 序列化)之间的混淆

  2. 如您所见,我想保留引用,但不包括“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:

  1. 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)

  2. 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 技术交流群。

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

发布评论

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

评论(1

-柠檬树下少年和吉他 2024-12-11 03:48:45

对于“您想要的”的几乎任何有用值,确实没有什么好的方法可以扩展 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 a ConnectionName property for the XmlSerializer to use, and locate the appropriate Connection either when ConnectionName is set or sometime after.

Alternatively, you'll need to have Table implement IXmlSerializable and completely hand-implement the code that generates the <Table> element.

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