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