This documentation is archived and is not being maintained.

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)

Public Event UnknownNode As XmlNodeEventHandler
Dim instance As XmlSerializer 
Dim handler As XmlNodeEventHandler 

AddHandler instance.UnknownNode, handler

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.

Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization

Public Class Group
   ' Only the GroupName field will be known. 
   Public GroupName As String 
End Class 

Public Class Test
   Shared Sub Main()
      Dim t As Test = New Test()
   End Sub 

   Private Sub DeserializeObject(filename As String )
      Dim mySerializer As XmlSerializer  = New XmlSerializer(GetType(Group))
      Dim fs As FileStream = New FileStream(filename, FileMode.Open)
      AddHandler mySerializer.UnknownNode, _
      AddressOf serializer_UnknownNode
      Dim myGroup As Group = _
      CType(mySerializer.Deserialize(fs), Group)
   End Sub 

   Private Sub serializer_UnknownNode _
   (sender As object , e As XmlNodeEventArgs )
      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)

      Dim myNodeType As XmlNodeType = e.NodeType
      Console.WriteLine("NodeType: {0}", myNodeType)

      Dim myGroup As Group = CType(e.ObjectBeingDeserialized, Group)
      Console.WriteLine("GroupName: {0}", myGroup.GroupName)
   End Sub 
End Class 
' Paste this XML into a file named UnknownNodes: 
'<?xml version="1.0" encoding="utf-8"?> 
'<Group xmlns:xsi="http:'"  

'xmlns:xsd="http:'" xmlns:coho = "http:'"  

'   <coho:GroupName>MyGroup</coho:GroupName> 
'   <cp:GroupSize>Large</cp:GroupSize> 
'   <cp:GroupNumber>444</cp:GroupNumber> 
'   <coho:GroupBase>West</coho:GroupBase> 
'   <coho:ThingInfo> 
'      <Number>1</Number> 
'      <Name>Thing1</Name> 
'      <Elmo> 
'         <Glue>element</Glue> 
'      </Elmo> 
'  </coho:ThingInfo> 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

public __gc class Group{
   // Only the GroupName field will be known.
   String* GroupName;

public __gc class Test{
   static void main(){
      Test* t = new Test();

   void DeserializeObject(String* filename){
      XmlSerializer* mySerializer = new XmlSerializer(__typeof(Group));
      FileStream* fs = new FileStream(filename, FileMode::Open);
      mySerializer->UnknownNode += new 
         XmlNodeEventHandler(this, &Test::serializer_UnknownNode);
      Group* myGroup = dynamic_cast<Group*> (mySerializer->Deserialize(fs));
   void serializer_UnknownNode
      (Object* /*sender*/, XmlNodeEventArgs* e){
         Console::WriteLine(S"UnknownNode Name: {0}",e->Name);
         Console::WriteLine(S"UnknownNode LocalName: {0}",e->LocalName);
         Console::WriteLine(S"UnknownNode Namespace URI: {0}",e->NamespaceURI);
         Console::WriteLine(S"UnknownNode Text: {0}",e->Text);

         XmlNodeType myNodeType = e->NodeType;
         Console::WriteLine(S"NodeType: {0}", __box(myNodeType));

         Group* myGroup = dynamic_cast<Group*> (e->ObjectBeingDeserialized);
         Console::WriteLine(S"GroupName: {0}",myGroup->GroupName);

int main()
/* Paste this XML into a file named UnknownNodes:
<?xml version="1.0" encoding="utf-8"?>
<Group xmlns:xsi="" 

xmlns:xsd="" xmlns:coho = "" 


Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0