Export (0) Print
Expand All

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 XmlNodeEventHandler UnknownNode

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;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

public class Group{
   // Only the GroupName field will be known. 
   public string GroupName;

public class Test{
   static void Main(){
      Test t = new Test();

   private void DeserializeObject(string filename){
      XmlSerializer mySerializer = new XmlSerializer(typeof(Group));
      FileStream fs = new FileStream(filename, FileMode.Open);
      mySerializer.UnknownNode += new 
      Group myGroup = (Group) mySerializer.Deserialize(fs);
   private void serializer_UnknownNode
   (object sender, XmlNodeEventArgs e){
      ("UnknownNode Name: {0}", e.Name);
      ("UnknownNode LocalName: {0}" ,e.LocalName);
      ("UnknownNode Namespace URI: {0}", e.NamespaceURI);
      ("UnknownNode Text: {0}", e.Text);

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

      Group myGroup = (Group) e.ObjectBeingDeserialized;
      Console.WriteLine("GroupName: {0}", myGroup.GroupName);
/* 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.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

© 2014 Microsoft