Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
UnknownNode Event
Collapse the table of content
Expand the table of content

XmlSerializer::UnknownNode Event

Occurs when the XmlSerializer encounters an XML node of unknown type during deserialization.

Namespace:  System.Xml.Serialization
Assembly:  System.Xml (in System.Xml.dll)

 event XmlNodeEventHandler^ UnknownNode {
	void add (XmlNodeEventHandler^ value);
	void remove (XmlNodeEventHandler^ value);

By default, after calling the Deserialize method, the XmlSerializer ignores XML nodes of unknown types. However, you can use this event to handle such node types.

The following example prints the type of any encountered unknown node.

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

public ref class Group

   // Only the GroupName field will be known.
   String^ GroupName;

public ref class Test
   static void main()
      Test^ t = gcnew Test;
      t->DeserializeObject( "UnknownNodes.xml" );

   void DeserializeObject( String^ filename )
      XmlSerializer^ mySerializer = gcnew XmlSerializer( Group::typeid );
      FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
      mySerializer->UnknownNode += gcnew XmlNodeEventHandler( this, &Test::serializer_UnknownNode );
      Group^ myGroup = dynamic_cast<Group^>(mySerializer->Deserialize( fs ));

   void serializer_UnknownNode( Object^ /*sender*/, XmlNodeEventArgs^ e )
      Console::WriteLine( "UnknownNode Name: {0}", e->Name );
      Console::WriteLine( "UnknownNode LocalName: {0}", e->LocalName );
      Console::WriteLine( "UnknownNode Namespace URI: {0}", e->NamespaceURI );
      Console::WriteLine( "UnknownNode Text: {0}", e->Text );
      XmlNodeType myNodeType = e->NodeType;
      Console::WriteLine( "NodeType: {0}", myNodeType );
      Group^ myGroup = dynamic_cast<Group^>(e->ObjectBeingDeserialized);
      Console::WriteLine( "GroupName: {0}", myGroup->GroupName );

int main()

/* Paste this XML into a file named UnknownNodes:
<?xml version="1.0" encoding="utf-8"?>
<Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:coho = "http://www.cohowinery.com" 


.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0
© 2015 Microsoft