使用 XmlSeralizer 在 C# 中解析稍微不正常的 XML
我收到了一些不太具有适当架构的“XML”文件(我认为这就是问题所在),并且生成它们的医疗设备无法更改为生成易于解析的 XML。 (通过如此诱人的小修改(在 Image 条目周围额外包裹 Images 标签),读取这些文件将变得微不足道——这不是 XML 的意义所在吗? ?)
基本上我被困在这里了。 XML 如下所示:(
<Series>
<Metadata1>foo</Metadata1>
<Metadata2>bar</Metadata2>
...
<Image>...</Image>
<Image>...</Image>
...
</Series>
可以有任意数量的图像,但可能的元数据标签都是已知的)。我的代码如下所示:
public class Image { ... }
public class Series : List<Image>
{
public Series() { }
public string Metadata1;
public string Metadata2;
...
}
当我像这样运行时:
XmlSerializer xs = new XmlSerializer(typeof(Series));
StreamReader sr = new StreamReader(path);
Series series = (Series)xs.Deserialize(sr);
sr.Close();
图像对象列表正确读取到系列对象中,但没有读取 Metadata1/2/etc 字段(事实上,在调试器中浏览对象会显示所有元数据字段在“原始视图”类型的字段内部)。
当我更改代码:
public class Series // // removed this : List<Image>
{
public Series() { }
public string Metadata1;
public string Metadata2;
...
}
并在文件上运行阅读器时,我得到一个包含 Metadata1/2/etc 的系列对象。完美填充,但没有读取图像数据(显然)。
如何解析 Metadata1/2/etc。以及具有最少痛苦的临时代码的一系列图像?
我是否必须编写一些自定义(痛苦?简单?)ReadXML 方法来实现 IXMLSeralizable?
我不太关心对象的布局方式,因为使用这些 C# 类的软件非常灵活:
List<Image> Images;for the images would be fine, or perhaps the metadata is wrapped in some object, whatever...
I've been given some "XML" files that don't quite have a proper schema (I think that's the problem) and the medical device that generates them cannot be changed to generate easy-to-parse XML. (With such a tantalizingly small modification (extra wrapping Images tags around the Image entries) it would be trivial to read these files---isn't that what XML is about?)
Basically I'm stuck here. The XML looks like this:
<Series>
<Metadata1>foo</Metadata1>
<Metadata2>bar</Metadata2>
...
<Image>...</Image>
<Image>...</Image>
...
</Series>
(there can be any number of images but the possible Metadata tags are all known). My code looks like this:
public class Image { ... }
public class Series : List<Image>
{
public Series() { }
public string Metadata1;
public string Metadata2;
...
}
When I run this like so:
XmlSerializer xs = new XmlSerializer(typeof(Series));
StreamReader sr = new StreamReader(path);
Series series = (Series)xs.Deserialize(sr);
sr.Close();
the List of Image objects reads properly into the series object but no Metadata1/2/etc fields are read (in fact, browsing the object in the debugger shows all of the metadata fields inside of a "Raw View" sort of field).
When I change the code:
public class Series // // removed this : List<Image>
{
public Series() { }
public string Metadata1;
public string Metadata2;
...
}
and run the reader on the file, I get a series object with Metadata1/2/etc. filled in perfectly but no Image data being read (obviously).
How do I parse both Metadata1/2/etc. and the series of Images with the least amount of painful ad hoc code?
Do I have to write some custom (painful? easy?) ReadXML method to implement IXMLSeralizable?
I don't care too much how the objects are laid out since my software that consumes these C# classes is totally flexible:
List<Image> Images;
for the images would be fine, or perhaps the metadata is wrapped in some object, whatever...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的类缺少允许 XML 的属性序列化工作。我相信以下内容应该足够了。
我不确定您是否可以使用通用类型来代替图像数组,但上面引用的链接应该为您提供有关如何针对特定情况应用序列化属性的更多信息。
编辑:另一种选择是手工制作 XML 架构来验证应用程序生成的文档,然后使用 XSD.exe 生成对象模型。生成的类将演示如何调整对象模型以与序列化器一起使用。
Your classes are missing the attributes that allow XML serialization to work. I believe the following should suffice.
I'm not sure if you can use a generic type in place of the image array, but the above referenced link should give you more information on how to apply the serialization attributes for you specific situation.
EDIT: Another option is to hand-craft and XML schema that will validate the documents produced by the application, then use XSD.exe to generate the object model. The resulting classes will demonstrate how you should tweek your object model to work with the serializer.
为什么要尝试使用 XML 序列化程序来执行此操作?序列化通常是指能够以某种众所周知的格式(文本或二进制)保存对象的“状态”,以便可以在以后的某个时间点重新创建它。这听起来不像你在这里想做的事情。这里的问题是 XML 数据与您的对象层次结构并不真正匹配。
您有一个硬件设备,可以以某种方式生成您想要使用的 XML 数据。对我来说,使用简单的 XmlDocument 或 XmlReader 类而不是尝试通过序列化器是最简单的。
您可能可以使用如下代码来做到这一点:
*这是未经测试/未经验证的代码,但它应该能够传达这个想法。
Why are you trying to use an XML serializer to do this? Serialization is generally about being able to save the "state" of an object in some well-known format (text or binary) so that it can be recreated at a later point in time. That doesn't sound like what you are trying to do here. The problem here is that the XML data doesn't really match your object hierarchy.
You have a hardware device that somehow generates XML data that you want to consume. To me, this would be easiest using a simple XmlDocument or XmlReader class rather than trying to go through the serializer.
You could probably do this with code like this:
*This is untested/unverified code, but it should get the idea across.
我认为史蒂夫的回答应该有效。我只是想补充一点,使用这种技术只能读取有限数量的元数据元素,因为它们没有常量名称。您可以做的是将它们读入 XmlElements 集合中,以便稍后解析:
I think Steve's answer should work. I just want to add that you can only read a finite number of Metadata elements with this technique, because they don't have a constant name. What you could do is read them into a collection of XmlElements that you can parse later :