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
/** @event */
public void add_UnknownNode (XmlNodeEventHandler value)

/** @event */
public void remove_UnknownNode (XmlNodeEventHandler value)

JScript supports the use of events, but not the declaration of new ones.

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();
      t.DeserializeObject("UnknownNodes.xml");
   }

   private void DeserializeObject(string filename){
      XmlSerializer mySerializer = new XmlSerializer(typeof(Group));
      FileStream fs = new FileStream(filename, FileMode.Open);
      mySerializer.UnknownNode += new 
      XmlNodeEventHandler(serializer_UnknownNode);
      Group myGroup = (Group) mySerializer.Deserialize(fs);
      fs.Close();
   }
   private 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 = (Group) e.ObjectBeingDeserialized;
      Console.WriteLine("GroupName: {0}", myGroup.GroupName);
      Console.WriteLine();
   }
}
/* 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" 

xmlns:cp="http://www.cpandl.com">
   <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>
</Group>
*/   

import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Serialization.*;

public class Group
{
    // Only the GroupName field will be known.
    public String groupName;
} //Group

public class Test
{
    public static void main(String[] args)
    {
        Test t = new Test();
        t.DeserializeObject("UnknownNodes.xml");
    } //main

    private void DeserializeObject(String filename)
    {
        XmlSerializer mySerializer = new XmlSerializer(Group.class.ToType());
        FileStream fs = new FileStream(filename, FileMode.Open);
        mySerializer.add_UnknownNode(new XmlNodeEventHandler
            (SerializerUnknownNode));
        Group myGroup = (Group)mySerializer.Deserialize(fs);
        fs.Close();
    } //DeserializeObject

    private void SerializerUnknownNode(Object sender, XmlNodeEventArgs e)
    {
        Console.WriteLine("UnknownNode Name: {0}", e.get_Name());
        Console.WriteLine("UnknownNode LocalName: {0}", e.get_LocalName());
        Console.WriteLine("UnknownNode Namespace URI: {0}",
            e.get_NamespaceURI());
        Console.WriteLine("UnknownNode Text: {0}", e.get_Text());

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

        Group myGroup = (Group)e.get_ObjectBeingDeserialized();
        Console.WriteLine("GroupName: {0}", myGroup.groupName);
        Console.WriteLine();
    } //SerializerUnknownNode
} //Test
/* 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" 

xmlns:cp="http://www.cpandl.com">
   <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>
</Group>
*/

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft