Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 687229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:06:19+00:00 2026-05-14T02:06:19+00:00

I have an XmlSerializer object, and I have added 2 event handlers to the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:06:20+00:00Added an answer on May 14, 2026 at 2:06 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following object: public class MyClass { public int Id { get;
I have an XmlSerializer which I use to Serialize an object to an XDocument.
I have problem with deserialize document to object using XmlSerializer class. Code my function
I am using the XmlSerializer and have the following property in a class public
With the XmlSerializer I can have my members in different namespaces to the parent
I'm getting an odd result when serializing a DateTime field using XmlSerializer. I have
I have a hierarchy of classes that are serialised to XML using XMLSerialiser .
I have an object that holds an object that is definded as IEnumerable, i.e.
I have an object like this, public class UserObj { public string First {get;
I have this method to Serialize an object with any type public static string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.