views:

121

answers:

2

I have an XmlSerializer object, and I have added 2 event handlers to the UnknownElement and UnknownAttribute events, as below:

XmlSerializer xs = new XmlSerialiser(typeof(MyClass));
xs.UnknownAttribute += new XmlAttributeEventHandler(xs_UnknownAttribute);
xs.UnknownElement += new XmlElementEventHandler(xs_UnknownAttribute);

Each of these event handlers basically do the same thing, they print out the node name or the attribute name causing the issue.

But for some reason, an InvalidOperationException is getting thrown saying there is an error in the xml document with . I thought these errors would be caught by my events?

Update

The exceptions are:

The exception is: Unhandled Exception: System.InvalidOperationException: There is an error in XML document (5, 110).

There is an InnerException of type XmlException, which states The 'MyTag' start tag on line 5 does not match the end tag of 'AnotherTag'. Line 5, position 110.

A: 

The two events you're handling have nothing to do with errors in the XML document structure.

I'll try to help you with the specific problem once you post the specific exception. You might even need to post the XML.


From the looks of the partial exception you posted, it appears that your document is invalid XML (mismatched tags). There is no way to detect this short of catching an exception.

John Saunders
@John I've posted the exceptions in an Update to the OP. Is there a way for me to detect errors in the xml doc structure without having to catch Exceptions?
AndyC
can you post the stack trace of the exception ?
remi bourgarel
+1  A: 

Without seeing the definition of MyClass and the XML that you're trying to read in, it's hard to give a definitive answer. That said, the text of the exception is quite obvious, the XML markup is malformed, rather than containing an unknown element or attribute, for example:

<AnotherTag>
  <MyTag>
  </AnotherTag>   <--- This should be </MyTag>
</MyTag>          <--- This should be </AnotherTag>

UnknownAttribute/UnknownElement handlers won't capture this because the structure of the XML is fundementally wrong. These events can't be called until the XML document has been sucessfully parsed into a tree of nodes, child nodes, attributes and so on.

Just to further explain the bit about UnknownAttribute/UnknownElement; if your class/XML was only allowed to contain elements called Field1 and Field2 then you'd find the UnknownElement event raised if you had an element called Field3 in your XML. The InvalidOperationException is raised because the XML isn't XML, the UnknownElement event is raised because there's an element in the XML that is unexpected, though the XML is otherwise valid.

Rob
@Rob Yep, I can't post the xml unfortunately, though I don't think it's required anyway. Basically what I need to do is for it to tell me where it's malformed, even if it's malformed in multiple places.When it throws the exception it will stop reading I assume, so if there are further errors these will not be found until a subsequent run, right?
AndyC
@AndyC - it's told you precisely where in the exception.. :) Once it hits that, all bets are off really as any further attempt to parse the document could result in *false positives* due to the previous error, so your "where ... even if .. in multiple places" is quite a difficult thing to ask, certainly not something I'm aware of the .net framework providing natively.
Rob
@Rob ah, well said! Thanks for the answer, appreciated.
AndyC