如何在 C# 中将两个不同的 xml 文件反序列化为两个不同的类对象?
假设我在同一个程序集中有两个不同的 xml 文件作为嵌入资源:
x.xml
<car brand="Hummer">
<type ... />
<chasis ... />
</car>
y.xml
<shark species="HammerHead">
<color ... />
<maxLen .... />
</shark>
我有两个类 Car.cs
和 Shark.cs
来帮助反序列化它们。
将它们反序列化为两个不同且独立的对象的技术是什么?
以下代码一次只能处理一种类型。不是吗?
string[] manifestResourceNames = assembly.GetManifestResourceNames();
foreach (string mrn in manifestResourceNames)
{
Stream stream = assembly.GetManifestResourceStream(mrn);
XmlSerializer serializer = new XmlSerializer(typeof(Car));
Car car = (Car)serializer.Deserialize(stream);
.... .... ....
}
并且,当此代码遇到 Shark
类时,它将生成异常。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了使系统在任何方面都可靠,您需要为 XML 命名空间(无论如何,您应该始终为 XML 命名空间 - 但我会省去您的咆哮)。因此:
您的 C# XML 序列化属性将变为:
接下来,您将按照
XmlSerializerManager
的方式编写一些内容。这将维护一个内部Dictionary, XmlSerializer>
- 您可以通过反射填充它(查找应用了XmlRootAttribute
的所有类型并创建元组根据Namespace, LocalName
并实例化该类型的XmlSerializer
)。这可能是一个静态类。要反序列化任何元素,您只需在字典中查找其名称和命名空间即可检索
XmlSerializer
实例。例如:For the system to be reliable in any way you need to namespace your XML (you should always be namespacing XML anyway - but I'll save you the rant). Therefore:
Your C# XML serialization attributes would then become:
Following this you would write something along the lines of a
XmlSerializerManager
. This would maintain an internalDictionary<Tuple<string, string>, XmlSerializer>
- which you can populate via reflection (look for all types with aXmlRootAttribute
applied and create the tuple according toNamespace, LocalName
and instantiate theXmlSerializer
for that type). This would probably be a static class.To deserialize any element you simply need to look up its Name and Namespace in the dictionary to retrieve the
XmlSerializer
instance. For example:查看 XmlSerializer 类。来自 MSDN:
如果您的类结构与给定的 ML 不完全匹配,即属性名称和 XML 元素之间没有相关性,您将 必须使用属性来提供合适的映射。
关于上面的代码,构造了一个
XmlSerializer
实例,以便序列化/反序列化单个类型。您需要创建此类的单独实例,一个用于汽车,一个用于鲨鱼。Have a look at the XmlSerializer class. From MSDN:
If you class structure doesn't not exactly match the ML given, i.e. there isn't a correlation between property names and XML elements, you will have to use attributes to provide a suitable mapping.
Regarding your code above, an
XmlSerializer
instance is constructed such that it serializes / de-serializes a single type. You need to create separate instances of this class, one for car one for shark.您的代码只能处理一个类。
我使用这个链接来了解XMLserializer。在这里你可以找到一些很好的例子。在最后一个示例中出现此代码:
您可以重建它以使用您的 .xml 文件:
通过调用这两个函数,您可以获得这两个类
your code can handle one class only.
i used this link to learn about XMLserializer. here you can find some nice examples. in the last example this code appears:
you can rebuild it to use your .xml files in this matter:
by calling these 2 function you can get both classes
我将创建一个如下所示的抽象基类
您可以轻松地从任何子类访问静态方法
希望这就是您正在寻找的。
托尔斯滕
I would create an abstract base class like the following
You could easily access the static method from any subclass
Hope that's what you're lookin for.
Thorsten