这是使用我不完全理解的第 3 方对象的序列化 XML 的合理方法吗?
我有来自第 3 方应用程序的序列化实例数据,它采用 XML 格式,我想将其加载到对象中并使用它。
我根据 XML 生成了一个 XSD,效果很好。 XML 表示用户填写的表单的状态。我第一次生成 XSD 时,没有填写整个表单 - 表单实例是序列化的。我返回并填写了整个表单,以便提供我需要的所有控件,并创建一个新的 XSD,并使用 XSD2Code 生成一个新类。
问题是,如果我不知道表单本身的所有序列化是什么样子 - 我的方法从一开始就有缺陷还是我能做的最好的方法?
将接口粘贴到生成的类上所需的控件上是否是减轻底层实例数据特征变化的好方法?
我现在知道我至少需要能够处理形式中的某些东西,并且它们都存在。如果将来向表单中添加更多内容,我可以重新生成 XSD 并在现有实现的基础上进行构建。
这是一种合理的方法吗,因为我将创建一个部分类,它在生成的类上指定它们至少实现我需要的东西?
您可以预见,是否有任何事情会对我编写的与实例类中的这些接口类型一起使用的代码提出挑战?
有没有办法让我的反序列化方法在出现意外情况时不会中断?
I have Serialized Instance Data from a 3rd party application, it is in XML and I want to load it into an object and work with it.
I generated a XSD off the the XML and it works great. The XML represents the state of a form the user filled out. The first time I generated the XSD, I hadn't filled out the whole form -the form instance being what is serialized. I went back and filled out the whole form, so that all the controls I need are present, and created a new XSD, and used XSD2Code to generate a new class.
Question is, if I don't know what all of the serialization looks like for the form itself - is my approach flawed from the beginning or is it the best that I can do?
Is sticking interfaces on the Controls I need on the generated classes a good way to mitigate changes in the characteristics of the underlying instance data?
I know now that I at least need to be able to work with certain things in the form and they are all present. If in the future more stuff is added to the form, I can regenerate the XSD and build on already present implementation.
Is this a sound approach, in that I will create a partial class which specifies on the generated classes that they at least implement the things I need?
Is there anything you can foresee that will challenge the code that I write that works with these interfaced types in the instance class?
Is there a way to have my de-serialization methods, not break if theres stuff that it isn't expecting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无知通常不是最佳做法。
如果他们向您发送 XML,那么他们不知道它是什么形状吗?他们没有 XSD 吗?
至少他们不能给你提供一份格式的文档吗?
无论如何,您需要一个 XSD 来表示他们实际将向您发送的内容,以便您可以生成与您实际收到的内容相匹配的类。您甚至可能会发现他们发送给您的 XML 结构与任何可能的类集都不匹配,并且您需要“手动”反序列化。
Ignorance is generally not a best practice.
If they are sending you the XML, then don't they know what shape it is? Don't they have an XSD?
At the very least, can't they provide you with a document on the format?
In any case, you need an XSD that represents what they will actually be sending you, so you can generate classes that match what you will actually receive. It's even possible that you'll find that the XML structure they are sending you does not match any possible set of classes, and that you will need to deserialize "manually".