外部 Xml 到 C# 对象,最简单的方法是什么?
我们从外部程序生成了 XML,需要将其读入我们的 C# 应用程序。
我知道我们可以使用带有 [Serializing]
标记的 C# 类来将其加载到对象中,但问题是我们已经拥有了比 Xml 更加面向对象的对象。我们不想有 2 组对象。
我的想法是,我可以使用 LinqToXml 打开该文件并填充我们的对象。这样做可以吗?或者真正的最佳方法是在我们的 OO 模型中使用带有 XmlElementAttribute
的 [Serializing]
标记和其他标记? (如果最新的是好的解决方案,我如何将属性放在对象内部的对象中或使用继承?)。
编辑:示例
假设我有:
public class Movie {
public string Name;
public Actor LeadActor;
}
public class Actor {
public string Name;
public DateTime DOB;
}
并且我想像这样序列化电影:
<Movie>
<Name>Casino Royale</Name>
<LeadActor>Daniel Craig</LeadActor>
</Movie>
我认为使其工作的唯一方法是修改电影以具有 LeadActor 属性,该属性将连接 Actor 属性并使两个 Actor 属性与[XmlIgnore] 属性。但是如果我不想改变这些业务对象怎么办?
We have XML generated from an external program that needs to be read into our C# application.
I know we could use C# classes with [Serializable]
tag to load the into objects but the problem is that we already have our object that is really more OO divised than the Xml. We do not want to have 2 sets of objects.
My though is that I could open the file with LinqToXml and fill up our objects. Is it okay to do it this way or the real best way is to use the [Serializable]
tag with XmlElementAttribute
and other tag into our OO model? (If the latest is the good solution, how can I put an attribute in object inside my object or to work with inheritance?).
Edit : Example
Let say that I have :
public class Movie {
public string Name;
public Actor LeadActor;
}
public class Actor {
public string Name;
public DateTime DOB;
}
And I want to serialize the Movie like this:
<Movie>
<Name>Casino Royale</Name>
<LeadActor>Daniel Craig</LeadActor>
</Movie>
The only way I see to make it works is to modify the Movie to have a LeadActor property that will concatenare the Actor property and make both Actor property with [XmlIgnore] attribute. But what if I do not want to alter those business object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果 xml 数据的结构已知(并且不太复杂),我发现使用 Xml.Linq 是最简单的方法。每个类都包含公共静态方法来从 xml 重建自身。您必须手动执行此操作,但如果 xml 结构与对象结构不对应,这似乎是唯一的方法 - 而且这个方法非常简单。
对于您的示例,它将是:
If the structure of the xml data is known (and not too complex) I find using Xml.Linq the easiest way. Each class contains public static method to reconstruct itself from xml. You have to do it manually, but if the xml structure does not correspond to object structure that seems like the only way - and this one is quite simple.
For your example it would be:
[Serializable] 属性启用二进制序列化,而不是 Xml 序列化。要支持 XML 序列化,对象必须具有默认构造函数和公共可设置属性。如果您的对象模型满足此要求,我会考虑使用内置的 XML 序列化 - 从长远来看它可以节省时间。
[Serializable] attribute enabled binary serialization, not Xml serialization. To support XML serialization object must have default constructor and public settable properties. If your object model meets this requirement I would consider using built-it XML serialization - it saves time in the long run.
您可以在顶级对象上实现 IXmlSerialized进行一些自定义序列化。
You could implement IXmlSerializable on your top level object to do some custom serialization.
您可以使用 xslt 转换将源 xml 转换为与对象模型匹配的 xml 格式,然后直接将转换后的 xml 反序列化为对象。
You could use an xslt transformation to transform the source xml into the xml format that matches your object model, and then directly deserialize the transformed xml into objects.