C
cctv.star
I've got a class that encapsulates certain logs as an XmlDocument. I
need to implement 2 properties (Errors and Warnings), which should be
enumerable by foreach statement, and which should be implemented by
reading this XML document. I guess I need some kind of "enumerator
adaptor" in order to convert XmlNode objects into my "Message"
objects, and I need to feed this adaptor with the output of
XmlDocument.SelectNodes call. Something like this:
<code>
class CheckResults {
public MessageCollection Errors {
get { return new SomeNiftyEnumAdaptor(
_doc.SelectNodes("/some/message[type='error']"),
delegate () { some conversion code here} ); };
}
public MessageCollection Warnings {
get { return new SomeNiftyEnumAdaptor(
_doc.SelectNodes("/some/message[type='warning']"),
delegate () { some conversion code here} ); };
}
private XmlDocument _doc;
}
// client code:
foreach(Message msg in results.Errors) {
}
</code>
The trouble is, the code that I've written so far is quite a
screenful. Which is strange, considering that the only specific stuff
here is this conversion function, which constructs my custom Message
type from XmlNode - everything else is just boilerplate code.
So, what's the proper wat to do this in C#?
need to implement 2 properties (Errors and Warnings), which should be
enumerable by foreach statement, and which should be implemented by
reading this XML document. I guess I need some kind of "enumerator
adaptor" in order to convert XmlNode objects into my "Message"
objects, and I need to feed this adaptor with the output of
XmlDocument.SelectNodes call. Something like this:
<code>
class CheckResults {
public MessageCollection Errors {
get { return new SomeNiftyEnumAdaptor(
_doc.SelectNodes("/some/message[type='error']"),
delegate () { some conversion code here} ); };
}
public MessageCollection Warnings {
get { return new SomeNiftyEnumAdaptor(
_doc.SelectNodes("/some/message[type='warning']"),
delegate () { some conversion code here} ); };
}
private XmlDocument _doc;
}
// client code:
foreach(Message msg in results.Errors) {
}
</code>
The trouble is, the code that I've written so far is quite a
screenful. Which is strange, considering that the only specific stuff
here is this conversion function, which constructs my custom Message
type from XmlNode - everything else is just boilerplate code.
So, what's the proper wat to do this in C#?