在 IXmlSerialized 中查找引用

发布于 2024-08-16 22:40:05 字数 672 浏览 5 评论 0原文

我使用 IXmlSerialized 将特定类型的元素写入传出 xml。 我已经实现了该架构,并且正在写出这些项目。下面的代码是一个示例。

public void IXmlSerializable.WriteXml(XmlWriter writer) {
  // Write Out Class.
  foreach (var item in myItems) {
    DataContractSerializer ds = new DataContractSerializer(typeof(MyType));
    ds.WriteObject(writer, item);
  }
}

我遇到的问题是 MyType 被声明为使用引用,

[DataContract(IsReference = true)]
public class MyType { ...

因此当该项目已写入 xml 时,它需要是一个引用。

我如何知道引用是否已写入 xml? 我认为我必须忽略我明确无法控制的参考文献。这样我就可以创建我自己的引用 ID 并引用我自己的实例。

这显然是一个糟糕的黑客妥协,因为我正在复制不应该复制的引用。

有什么办法可以找出已经写入的内容,看看是否可以找到已序列化的项目的 id?

问候

克雷格。

I am writing elements of a particular type into the outgoing xml using IXmlSerializable.
I have implemented the schema, and am writing the items out. The code that follows is an example.

public void IXmlSerializable.WriteXml(XmlWriter writer) {
  // Write Out Class.
  foreach (var item in myItems) {
    DataContractSerializer ds = new DataContractSerializer(typeof(MyType));
    ds.WriteObject(writer, item);
  }
}

The problem I have is that MyType is declared as using references

[DataContract(IsReference = true)]
public class MyType { ...

So when the item has already been written to the xml it needs to be a reference.

How do I know if a reference has already been written to the xml?
I am of the opinion that I must just ignore references that I am explicitly not in control of. That way I will make up my own reference id's and reference my own instances.

This is clearly a bad hacked compromise as I am duplicating references that should not be duplicated.

Is there any way to find out what has already been written to see if I can find an id for the item already serialized?

Regards

Craig.

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

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

发布评论

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

评论(1

云裳 2024-08-23 22:40:05

“IsReference”魔法仅适用于单个“剧集”内的连载。一个情节是一次 WriteObject 调用。

假设您有一个某种顶级类型的顶级对象,如下所示:

[DC]    
class Container
    {
      [DM]
      MyType i1 = new MyType();
      [DM]
      MyType i2 = i1;;
      [DM]
      MyType i3 = i1;
    }

现在,如果您要通过调用 WriteObject 来序列化 Container 的实例,那就是“ids”和“refs”进入的时候玩。当 i1 被序列化时,它将使用 ID 1 进行序列化,但是当 i2 和 i3 被序列化时,它们都会使用指向 MyType 的 ID 1 的“REF”属性进行序列化。

在您的示例中,因为对 WriteObject 的每次调用都是一个单独的情节,所以每次调用都会序列化整个对象图。除非您可以将所有不同的 MyType 实例打包到更高级别的对象(甚至是集合)中,否则您就不走运了。这就是您需要做的——本质上强制 MyType 的所有实例在单个更高级别的 WriteObject 调用中序列化。

The "IsReference" magic works only for serializations within a single "episode". An episode is a single WriteObject call.

Let's say you were to have a top-level object of some top-level type, like below:

[DC]    
class Container
    {
      [DM]
      MyType i1 = new MyType();
      [DM]
      MyType i2 = i1;;
      [DM]
      MyType i3 = i1;
    }

Now, if you were to serialize an instance of Container via a call to WriteObject, that is when "ids" and "refs" come into play. When i1 gets serialized, it will get serialized with an ID of 1, but when i2 and i3 get serialized, they each get serialized with "REF" attributes pointing back to the ID 1 of MyType.

In your example, because every call to WriteObject is a separate episode, each call will serialize out the whole object graph. Unless you can package all of your different MyType instances into a higher-level object (or even a collection), you're out of luck. So that's what you need to do -- essentially force all instances of MyType to be serialized within a single higher level WriteObject call.

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