Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
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.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