与XmlSeralizer解析稍微偏离失衡的XML在C# [英] Parsing slightly off-kilter XML in C# with XmlSeralizer

查看:177
本文介绍了与XmlSeralizer解析稍微偏离失衡的XML在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经因为不太有一个适当的模式有些XML的文件(我认为这是问题),并生成它们不能被改变,以产生易于解析XML的医疗设备。 (有了这样一个诱人的小修改(额外包装图片围绕图片标记项)这将是微不足道的读取这些文件---这不正是XML是关于?)

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?)

基本上我困在这里。该XML看起来是这样的:

Basically I'm stuck here. The XML looks like this:

<Series>
   <Metadata1>foo</Metadata1>
   <Metadata2>bar</Metadata2>
   ...
   <Image>...</Image>
   <Image>...</Image>
   ...
</Series>

(可以有任何数目的图像,但是可能的元数据标签的所有已知的)。我的code是这样的:

(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;
	...
}

当我运行这个像这样:

    		XmlSerializer xs = new XmlSerializer(typeof(Series));
    		StreamReader sr = new StreamReader(path);
    		Series series = (Series)xs.Deserialize(sr);
    		sr.Close();

Image对象的名单读起来正确入编对象,但没有Metadata1 / 2 /等领域的读取(事实上,浏览对象中的调试器显示所有的原始视图排序字段内的元数据字段)。

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).

当我改变code:

public class Series    // // removed this : List<Image>
{
	public Series() { }
	public string Metadata1;
	public string Metadata2;
	...
}

和运行在该文件的读者,我与Metadata1 / 2 /等一系列的对象。填补了完美,但没有图像数据读取(显然)。

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).

我如何分析这两种Metadata1 / 2 /等。与该系列的最低金额痛苦的特设code形象?

How do I parse both Metadata1/2/etc. and the series of Images with the least amount of painful ad hoc code?

我是否需要写一些自定义的(痛苦?容易吗?)ReadXML的方法来实现IXMLSeralizable?

Do I have to write some custom (painful? easy?) ReadXML method to implement IXMLSeralizable?

我没有太在意的对象的方式,因为我的软件,消耗这些C#类布局是非常灵活的:

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...

推荐答案

您的类缺少的 ,使XML序列化的属性工作。我认为,以下就足够了。

Your classes are missing the attributes that allow XML serialization to work. I believe the following should suffice.

[XmlElement]
public class Image { ... }

[XmlRoot(ElementName="Series")]
public class Series
{
        public Series() { }

        [XmlElement]
        public string Metadata1;

        [XmlElement]
        public string Metadata2;

        [XmlElement(ElementName="Image")]
        public Image[] Images;
}

我不知道你是否能代替图像阵列中使用泛型类型,但上面提到的链接应该给你如何申请您的具体情况序列化属性的详细信息。

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.

编辑:另一种方法是手工工艺和XML模式,将验证应用程序产生的文件,然后使用的 XSD.EXE 生成对象模型。由此产生的类将展示你应该怎么tweek对象模型的串行工作。

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.

这篇关于与XmlSeralizer解析稍微偏离失衡的XML在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆